How do i bind function arguments to the parameters i supply in creating an ArgumentException object?

徘徊边缘 提交于 2019-12-11 07:39:37

问题


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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!