I\'m looking for a way to have a function such as:
myFunction({\"Key\", value}, {\"Key2\", value});
I\'m sure there\'s something with anonymous
Funny, I just created (minutes ago) a method that allows to do that, using anonymous types and reflection :
MyMethod(new { Key1 = "value1", Key2 = "value2" });
public void MyMethod(object keyValuePairs)
{
var dic = DictionaryFromAnonymousObject(keyValuePairs);
// Do something with the dictionary
}
public static IDictionary DictionaryFromAnonymousObject(object o)
{
IDictionary dic = new Dictionary();
var properties = o.GetType().GetProperties();
foreach (PropertyInfo prop in properties)
{
dic.Add(prop.Name, prop.GetValue(o, null) as string);
}
return dic;
}