Convert Dictionary To Anonymous Object?

后端 未结 7 1412
挽巷
挽巷 2020-11-27 16:33

First, and to make things clearer I\'ll explain my scenario from the top:

I have a method which has the following signature:

public virtual void Send         


        
7条回答
  •  盖世英雄少女心
    2020-11-27 16:44

    Slightly more modular version of svick's answer, using a couple extension methods:

    public static class Extensions
    {
        public static void AddRange(this ICollection collection, IEnumerable items)
        {
            foreach (var item in items)
            {
                collection.Add(item);
            }
        }
    
        public static dynamic ToDynamicObject(this IDictionary source)
        {
            ICollection> someObject = new ExpandoObject();
            someObject.AddRange(source);
            return someObject;
        }
    }
    

提交回复
热议问题