我需要转换JSON中的类,但我只需要基属性.
例如,在下面的类A中,我只需要前3个属性,但不能修改类(从数据库自动生成). 我已经创建了一个定制的JsonConverter,并用我的逻辑填充了方法CanConvert,但这不起作用,并将对象A的列表转换为OutOfMemory异常
class A {
string name {get; set;}
string surname {get; set;}
string roleCode {get; set;}
Role role {get; set;}
}
-----
public override bool CanConvert(Type objectType)
{
var isDate = objectType == typeof(DateTime) || objectType == typeof(DateTime?) || objectType == typeof(Nullable<DateTime>);
var isString = objectType == typeof(string);
var isClass = objectType.IsClass;
return !isClass && !(isDate || isString);
}