You can use an anonymous type and reflection as a workaround to named parameters:
public void Foo<T>(T parameters) { var dict = typeof(T).GetProperties() .ToDictionary(p => p.Name, p => p.GetValue(parameters, null)); if (dict.ContainsKey("Message")) { Console.WriteLine(dict["Message"]); } }
So now I can call Foo as follows:
Foo(new { Message = "Hello World" });
... and he will write my message.
Basically, I extract all the properties from the anonymous type that was passed in and convert them to a dictionary of string and object (property name and its value).
Matt hamilton
source share