Dynamically adding properties to a dynamic object?

后端 未结 3 822
半阙折子戏
半阙折子戏 2021-01-04 12:16

i have this

dynamic d = new ExpandoObject();
d.Name = attribute.QualifiedName.Name;

so , i know that d will have a property Name. Now if i

3条回答
  •  醉话见心
    2021-01-04 13:20

    You can also do like this:-

    Dictionary coll = new Dictionary();
        coll.Add("Prop1","hello");
        coll.Add("Prop2",1);
        System.Dynamic.ExpandoObject obj = dic.Expando();
    
    //You can have this ext method to better help
    
    public static ExpandoObject Expando(this IEnumerable> 
    dictionary)
            {
                var expando = new ExpandoObject();
                var expandoDic = (IDictionary)expando;
                foreach (var item in dictionary)
                {
                    expandoDic.Add(item);
                }
                return expando;
            }
    

提交回复
热议问题