如何绑定功能参数i电源在创造一个ArgumentException对象的参数? [英] How do i bind function arguments to the parameters i supply in creating an ArgumentException object?

查看:236
本文介绍了如何绑定功能参数i电源在创造一个ArgumentException对象的参数?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我经常带参数的像这样的功能:

I often have functions that take arguments like this:

公共共享函数f(BYVAL地址字符串)

所以基本上我想扔的新的ArgumentException(无效的地址!,地址)

现在的问题是,当我使用内置的重构工具来重命名局部变量,(比方说,我改名地址地址),在我的观点异常的描述仍是地址,而不是地址!

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"!

我不知道什么是对这个问题的最佳解决方案? (除手动追踪或使用Ctrl-F搜索)

I'm wondering what's the best solution to this problem? (other than manually tracking or with Ctrl-F search)

推荐答案

在C#中,你可以做这样的事情:

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)); 
    }
}

这篇关于如何绑定功能参数i电源在创造一个ArgumentException对象的参数?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆