How would you, in C#, determine the name of the variable that was used in calling a method?
Example:
public void MyTestMethod1()
{
string myVar = "Hello World";
MyTestMethod2(myVar);
}
public void MyMethod2(string parm)
{
// do some reflection magic here that detects that this method was called using a variable called 'myVar'
}
I understand that the parameter might not always be a variable, but the place I am using it is in some validation code where I am hoping that the dev can either explicitly state the friendly name of the value they are validating, and if they don’t then it just infers it from the name of the var that they called the method with…
Unfortunately, that’s going to be very hard, if not impossible.
Why exactly do you need the name of the variable originally sent in?
Remember that there might not be an actual variable used:
In this case, myVar holds just parts of the value, the rest is calculated in the expression at runtime.