目前,我偶然发现了newtonsofts json库的问题,这对我来说是一个谜。
我有几个类,这些类正在实现IdynamicMetaObjectProvider接口。将几个对象序列到JSON不是问题,我完全从对象的每个实例中获得了我期望的JSON。
但是,挑战使我头疼。从我到目前为止观察到的内容,似乎库正在缓存它无法找到的每个动态属性的值,并在应用程序运行时保留此功能。因此,例如,我正在遵循三个JSON:
{ "PropA": "1" }
{ "PropA": "2", "PropB": "1" }
{ "PropA": "3", "PropB": "2", "PropC": "1" }
可以选择这一点的jsons将为我遵循.net对象:
{ "PropA": "1" }
{ "PropA": "1", "PropB": "1" }
{ "PropA": "1", "PropB": "1", "PropC": "1" }
但是!如果我将目标类型从实现的iDynamicMetaObjectProvider更改为dicitionary或Dynamic,则测试对象将正确设置属性。
我的班级有一个索引属性,在设置器上设置了一个断点,该设置器已经提供了错误的值(因此,我的类实现都没有问题)。
public abstract class DynamicModelObject : IDynamicMetaObjectProvider //, IPropertyIndexer //, IDictionary<String, Object>
{
[NotMapped]
[JsonIgnore]
internal Dictionary<String, Object> properties = new Dictionary<String, Object>();
[IgnoreProperty]
[JsonIgnore]
public override Object this[String propertyName]
{
get
{
object val;
if (properties.TryGetValue(propertyName, out val)) {
return val;
}
var prop = this.GetType().GetProperty(propertyName);
if (prop != null && prop.CanRead) {
return prop.GetValue(this);
}
return null;
}
set
{
isDearty = true;
var prop = this.GetType().GetProperty(propertyName);
if (prop != null && prop.CanWrite) {
prop.SetValue(this, value);
} else {
properties[propertyName] = value;
}
var val = value as String;
if (value == null || (val != null && String.IsNullOrEmpty(val))) {
properties.Remove(propertyName);
}
}
}
public DynamicMetaObject GetMetaObject(System.Linq.Expressions.Expression parameter)
{
return new DynamicDictionaryPropertyStore<DynamicModelObject>(parameter, this);
}
[IgnoreProperty]
public IEnumerable<String> DynamicPropertyMemberNames
{
get
{
foreach (var key in properties.Keys) {
yield return key;
}
}
}
private List<String> staticProperties = null;
[IgnoreProperty]
private IEnumerable<String> StaticPropertyMemberNames
{
get
{
if (staticProperties == null) {
staticProperties = new List<String>();
foreach (var prop in this.GetType()
.GetProperties(System.Reflection.BindingFlags.Public | System.Reflection.BindingFlags.Instance | System.Reflection.BindingFlags.GetProperty)) {
if (!Attribute.IsDefined(prop, typeof(IgnorePropertyAttribute)) && !Attribute.IsDefined(prop, typeof(ScriptIgnoreAttribute))) {
staticProperties.Add(prop.Name);
yield return prop.Name;
}
}
} else {
foreach (var prop in staticProperties) {
yield return prop;
}
}
}
}
[IgnoreProperty]
private IEnumerable<String> AllPropertyMemberNames
{
get
{
foreach (var prop in DynamicPropertyMemberNames.Concat(StaticPropertyMemberNames)) {
yield return prop;
}
}
}
private class DynamicDictionaryPropertyStore<T> : DynamicMetaObject where T : DynamicModelObject
{
private T target;
internal DynamicDictionaryPropertyStore(System.Linq.Expressions.Expression parameter, T target)
: base(parameter, BindingRestrictions.Empty, target)
{
this.target = target;
}
public override IEnumerable<string> GetDynamicMemberNames()
{
return target.DynamicPropertyMemberNames;
}
public override DynamicMetaObject BindSetMember(SetMemberBinder binder, DynamicMetaObject value)
{
BindingRestrictions restrictions = BindingRestrictions.GetTypeRestriction(Expression, LimitType);
System.Linq.Expressions.Expression self = System.Linq.Expressions.Expression.Convert(Expression, LimitType);
if (binder == null) return null;
var body = System.Linq.Expressions.Expression.Property(self, "Item", System.Linq.Expressions.Expression.Constant(binder.Name));
var convert = System.Linq.Expressions.Expression.Convert(System.Linq.Expressions.Expression.Constant(value.Value), typeof(object));
var lambda = System.Linq.Expressions.Expression.Assign(body, convert);
return new DynamicMetaObject(lambda, restrictions);
}
public override DynamicMetaObject BindGetMember(GetMemberBinder binder)
{
BindingRestrictions restrictions = BindingRestrictions.GetTypeRestriction(Expression, LimitType);
System.Linq.Expressions.Expression self = System.Linq.Expressions.Expression.Convert(Expression, LimitType);
if (binder == null) return null;
var body = System.Linq.Expressions.Expression.Property(self, "Item", System.Linq.Expressions.Expression.Constant(binder.Name));
return new DynamicMetaObject(body, restrictions);
}
}
}
发生了什么?
您对DynamicModelObject
的实现不正确。在DynamicDictionaryPropertyStore
子类中,您进行以下操作:
public override DynamicMetaObject BindSetMember(SetMemberBinder binder, DynamicMetaObject value) {
BindingRestrictions restrictions = BindingRestrictions.GetTypeRestriction(Expression, LimitType);
System.Linq.Expressions.Expression self = System.Linq.Expressions.Expression.Convert(Expression, LimitType);
if (binder == null) return null;
var body = System.Linq.Expressions.Expression.Property(self, "Item", System.Linq.Expressions.Expression.Constant(binder.Name));
var convert = System.Linq.Expressions.Expression.Convert(System.Linq.Expressions.Expression.Constant(value.Value), typeof(object));
var lambda = System.Linq.Expressions.Expression.Assign(body, convert);
return new DynamicMetaObject(lambda, restrictions);
}
如果您要查看所产生的表达式,则会看到(例如PropA
):
Convert($arg0).Item["PropA"] = Convert("1")
因此,作为一个设置器,您返回调用索引器并分配常数值(1),而不管是什么值通过了什么值。此表达式将在稍后用于所有设置,用于PropA
(缓存)。因此,您的问题:所有的设定器都会忽略传递的值,并且将始终为您首次致电它们的值分配值。要修复,请更换此行:
var convert = System.Linq.Expressions.Expression.Convert(System.Linq.Expressions.Expression.Constant(value.Value), typeof(object));
与此:
var convert = System.Linq.Expressions.Expression.Convert(value.Expression, typeof(object));
由此产生的设置器表达式将是:
Convert($arg0).Item["PropA"] = Convert($arg1)
请注意,没有cast柱,只有争论。之后,您的问题将解决。