How can I use collection initializer syntax with ExpandoObject?

后端 未结 6 1056
温柔的废话
温柔的废话 2021-02-07 08:24

I\'ve noticed that the new ExpandoObject implements IDictionary which has the requisite IEnumerable

6条回答
  •  轮回少年
    2021-02-07 09:09

    It's a shame that adding dynamic properties (whose name is known only at run-time) to ExpandoObject is not as easy as it should have been. All the casting to dictionary is plain ugly. Never mind you could always write a custom DynamicObject that implements Add which helps you with neat object initializer like syntax.

    A rough example:

    public sealed class Expando : DynamicObject, IDictionary
    {
        readonly Dictionary _properties = new Dictionary();
    
        public override bool TryGetMember(GetMemberBinder binder, out object result)
        {
            return _properties.TryGetValue(binder.Name, out result);
        }
    
        public override bool TrySetMember(SetMemberBinder binder, object value)
        {
            if (binder.Name == "Add")
            {
                var del = value as Delegate;
                if (del != null && del.Method.ReturnType == typeof(void))
                {
                    var parameters = del.Method.GetParameters();
                    if (parameters.Count() == 2 && parameters.First().ParameterType == typeof(string))
                        throw new RuntimeBinderException("Method signature cannot be 'void Add(string, ?)'");
                }
            }
            _properties[binder.Name] = value;
            return true;
        }
    
    
    
        object IDictionary.this[string key]
        {
            get
            {
                return _properties[key];
            }
            set
            {
                _properties[key] = value;
            }
        }
    
        int ICollection>.Count
        {
            get { return _properties.Count; }
        }
    
        bool ICollection>.IsReadOnly
        {
            get { return false; }
        }
    
        ICollection IDictionary.Keys
        {
            get { return _properties.Keys; }
        }
    
        ICollection IDictionary.Values
        {
            get { return _properties.Values; }
        }
    
    
    
        public void Add(string key, object value)
        {
            _properties.Add(key, value);
        }
    
        bool IDictionary.ContainsKey(string key)
        {
            return _properties.ContainsKey(key);
        }
    
        bool IDictionary.Remove(string key)
        {
            return _properties.Remove(key);
        }
    
        bool IDictionary.TryGetValue(string key, out object value)
        {
            return _properties.TryGetValue(key, out value);
        }
    
        void ICollection>.Add(KeyValuePair item)
        {
            ((ICollection>)_properties).Add(item);
        }
    
        void ICollection>.Clear()
        {
            _properties.Clear();
        }
    
        bool ICollection>.Contains(KeyValuePair item)
        {
            return ((ICollection>)_properties).Contains(item);
        }
    
        void ICollection>.CopyTo(KeyValuePair[] array, int arrayIndex)
        {
            ((ICollection>)_properties).CopyTo(array, arrayIndex);
        }
    
        bool ICollection>.Remove(KeyValuePair item)
        {
            return ((ICollection>)_properties).Remove(item);
        }
    
        IEnumerator> IEnumerable>.GetEnumerator()
        {
            return _properties.GetEnumerator();
        }
    
        IEnumerator IEnumerable.GetEnumerator()
        {
            return ((IEnumerable)_properties).GetEnumerator();
        }
    }
    
    
    

    And you could call like you wanted:

    dynamic obj = new Expando()
    {
        { "foo", "hello" },
        { "bar", 42 },
        { "baz", new object() }
    };
    
    int value = obj.bar;
    

    The caveat with this approach is that you cannot add Add "method" with the same signature as Dictionary.Add to your expando object since it is already a valid member of the Expando class (which was required for the collection initializer syntax). The code throws an exception if you do

    obj.Add = 1; // runs
    obj.Add = new Action(....); // throws, same signature
    obj.Add = new Action(....); // throws, same signature for expando class
    obj.Add = new Action(....); // runs, different signature
    obj.Add = new Func(....); // runs, different signature
    

    If property names needn't be truly dynamic then another alternative is to have a ToDynamic extension method so that you can initialize in-line.

    public static dynamic ToDynamic(this object item)
    {
        var expando = new ExpandoObject() as IDictionary;
        foreach (var propertyInfo in item.GetType().GetProperties())
            expando[propertyInfo.Name] = propertyInfo.GetValue(item, null);
    
        return expando;
    }
    

    So you can call:

    var obj = new { foo = "hello", bar = 42, baz = new object() }.ToDynamic();
    
    int value = obj.bar;
    

    There are a hundred ways you can design an API for this, another one such (mentioned in orad's answer) is:

    dynamic obj = new Expando(new { foo = "hello", bar = 42, baz = new object() });
    

    Will be trivial to implement.


    Side note: there is always anonymous types if you know the property names statically and you dont want to add further after initialization.

    提交回复
    热议问题