问题
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)
回答1:
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));
}
}
回答2:
ReSharper can handle this. It can also rename occurrences of variables in strings.
However, I am not sure, how good their VB.NET support is.
来源:https://stackoverflow.com/questions/6042374/how-do-i-bind-function-arguments-to-the-parameters-i-supply-in-creating-an-argum