I often have functions that take arguments like this:
Public Shared Function F(ByVal address as String)
So basically I want to throw new ArgumentException("Invalid address!", "address")
Now problem is that when I'm using the built in refactoring tools to rename the local variables, (let's say i rename address to addr), The string in my argument exception is still "address" and not "addr"!
I'm wondering what's the best solution to this problem? (other than manually tracking or with Ctrl-F search)
In C#, you can do something like that:
static string GetMemberName<T>(Expression<Func<T>> expr)
{
var body = ((MemberExpression)expr.Body);
return body.Member.Name;
}
You would then use it like this:
static void Test(string someParam)
{
if (someParam == null) {
throw new ArgumentNullException(GetMemberName(() => someParam));
}
}