小编Max*_*ruk的帖子

通过JsonConvert和自定义TypeConverter反序列化对象

我有简单的DTO

public class SimpleDto
{
    public int Status { get; set; }
    public long FromDate { get; set; }
    public long ToDate { get; set; }
}
Run Code Online (Sandbox Code Playgroud)

我有ProxyDto TypeConverterAttribute:

[TypeConverter(typeof(SimpleConvert<SimpleDto>))]
public class ProxyDto<T>
{
    public T Object { get; set; }
}
Run Code Online (Sandbox Code Playgroud)

这是执行SimpleConvert:

public class SimpleConvert<T> : TypeConverter
{
    public override bool CanConvertFrom(ITypeDescriptorContext context, Type sourceType)
    {
        return sourceType == typeof(string) ||
                base.CanConvertFrom(context, sourceType);
    }

    public override object ConvertFrom(ITypeDescriptorContext context, CultureInfo culture, object value)
    {
        var strValue …
Run Code Online (Sandbox Code Playgroud)

c# json json.net

8
推荐指数
1
解决办法
2434
查看次数

标签 统计

c# ×1

json ×1

json.net ×1