I have a method that returns an object of dynamically created and would like to add the keys of a dictionary as the dynamic properties of this object and dictionary values as values of the properties of this new object. Below is the method but am having no success, someone help me understand and do this?
*is not a dictionary object is an object with values (key and value) of a dictionary
public Object setObjectProperty(Dictionary<String, String> dictionary)
{
dynamic newObj = new ExpandoObject();
foreach (var word in dictionary)
{
newObj.word.Key = word.Value;
}
return (Object) newObj;
}
ExpandoObject
implements IDictionary<string, object>
, but it implements those methods explicitly. Simply cast it to that type and then you can access the Add
method:
public static Object setObjectProperty(Dictionary<String, String> dictionary)
{
IDictionary<string, object> newObj = new ExpandoObject();
foreach (var word in dictionary)
{
newObj.Add(word.Key, word.Value);
}
return newObj;
}
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.