When I am tracing, I think it would be useful to do something like
//In the main function
{
Log(myVariable);
}
Which sends the variable to a generic function like this
//In the Logger file
public static void TraceMessage<T>(T aVariable)
{
string oldName=GetOldName(aVariable);
}
I want "myVariable" to be assigned to oldName
. What should GetOldName
do?
Something similar was asked here:
get name of a variable or parameter
But in all of those cases, "aVariable" is assigned to oldName
.
Update: Old name is what the parameter/variable was called before it was sent to the function. I use it as a variable here just for ease of explaining. The reason for this is debugging. When my program receives an error I would like to know what the value of my variables are. I currently have to send Log(the error, the variable name, the variable value). When you write 1000 of the these Debug statements you think of ways this could be simplified. What I am asking would simplify the problem.
Why did my question get downvoted and how can I improve the question?
This information needs to be captured and provided by the caller. In C# 6, it can be easily achieved using the
nameof
operator, although you'll need to apply this in your caller code:Edit: If you only want to specify your variable once, you can use:
And define your
Log
method as:However, this will be much slower than the alternative, and is not guaranteed behaviour.