Convert Dictionary To Anonymous Object?

后端 未结 7 1413
挽巷
挽巷 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:54

    If you really want to convert the dictionary to an object that has the items of the dictionary as properties, you can use ExpandoObject:

    var dict = new Dictionary { { "Property", "foo" } };
    var eo = new ExpandoObject();
    var eoColl = (ICollection>)eo;
    
    foreach (var kvp in dict)
    {
        eoColl.Add(kvp);
    }
    
    dynamic eoDynamic = eo;
    
    string value = eoDynamic.Property;
    

    But I'm not sure how is doing that going to help you.

提交回复
热议问题