Newtonsoft问题IDynamicMetaObjectProvider在反序列化时实现类

时间:2017-03-02 14:18:53

标签: c# json dynamic json.net

目前我偶然发现了Newtonsofts Json图书馆的一个问题,这对我来说是个谜。

我有一些正在实现IDynamicMetaObjectProvider接口的类。将几个对象序列化为json是没有问题的,我得到了我期望从对象的每个实例得到的json。

然而,反序列化给我带来了麻烦。从我到目前为止所看到的情况来看,似乎库正在缓存它在应用程序运行时无法找到并保留的每个动态属性的值。例如,我有以下三个Jsons:

{ "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,或者只是动态,则反序列化的对象将正确设置属性。

我的类有一个索引属性,在setter上设置一个断点,已经为setter提供了错误的值(所以我的类的实现没有问题)。

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);
            }
        }
    }

怎么回事?

1 个答案:

答案 0 :(得分:2)

您对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")

因此,作为一个setter,您将返回调用索引器的表达式并指定常量值(1),而不管实际传递的是什么值。此表达式稍后将用于PropA(缓存)的所有setter。因此,您的问题是:所有的setter都会忽略传递的值,并且总是会分配您第一次调用它们的值。要修复,请替换此行:

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));

得到的setter表达式为:

Convert($arg0).Item["PropA"] = Convert($arg1)

请注意,没有costants,只有参数。之后你的问题就会解决。