I thought I could make use of the new c# 6 operator nameof to build a dictionary of key/values implicitly from a params array.
As an example, consider the following method call:
string myName = "John", myAge = "33", myAddress = "Melbourne";
Test(myName, myAge, myAddress);
I am not sure there will be an implementation of Test that will be able to imply the name of the elements, from the params array.
Is there a way to do this using just nameof
, without reflection ?
private static void Test(params string[] values)
{
List<string> keyValueList = new List<string>();
//for(int i = 0; i < values.Length; i++)
foreach(var p in values)
{
//"Key" is always "p", obviously
Console.WriteLine($"Key: {nameof(p)}, Value: {p}");
}
}
nameof
is a compiler time facility, not a runtime one. – LotterymyName
,myAge
, etc. to exist in the compiled IL. If it doesn't exist, it can't be got. – Delinda