有没有办法从被调用方法中的调用方法获取参数名称?
Is there a way to get name of an argument from calling method in called method?
以下代码打印 "argument".
void PrintNameOf(string argument)
{
Console.WriteLine($"{nameof(argument)} has value: {argument}");
}
string myString = "hello";
PrintNameOf(myString);
有没有办法得到"myString"?
你必须做
void PrintNameOf(string argument, string name)
{
Console.WriteLine($"{name} has value: {argument}");
}
string myString = "hello";
PrintNameOf(myString, nameof(myString));
以下代码打印 "argument".
void PrintNameOf(string argument)
{
Console.WriteLine($"{nameof(argument)} has value: {argument}");
}
string myString = "hello";
PrintNameOf(myString);
有没有办法得到"myString"?
你必须做
void PrintNameOf(string argument, string name)
{
Console.WriteLine($"{name} has value: {argument}");
}
string myString = "hello";
PrintNameOf(myString, nameof(myString));