Add property to ExpandoObject with the same name as a string

前端 未结 2 1884
萌比男神i
萌比男神i 2021-02-01 15:55

Is there a way to add a property to an ExpandoObject with the same name as a string value?

For example, if I have:

string propName = \"ProductNumber\";
d         


        
2条回答
  •  深忆病人
    2021-02-01 16:21

    Cast the ExpandoObject to an IDictionary to do this:

    string propName = "ProductNumber";
    dynamic obj = new System.Dynamic.ExpandoObject();
    var dict = (IDictionary)obj;
    dict[propName] = 123;
    

提交回复
热议问题