相关疑难解决方法(0)

是否可以重构此扩展方法?

我有以下扩展方法:

public static void ThrowIfArgumentIsNull<T>(this T value, string argument) 
    where T : class
{
    if (value == null)
    {
        throw new ArgumentNullException(argument);
    }
}
Run Code Online (Sandbox Code Playgroud)

这是它的用法的一个例子....

// Note: I've poorly named the argument, on purpose, for this question.
public void Save(Category qwerty)
{
    qwerty.ThrowIfArgumentIsNull("qwerty");
    ....
}
Run Code Online (Sandbox Code Playgroud)

工作100%罚款.

但是,我不喜欢我必须提供变量的名称,只是为了帮助我的异常消息.

我想知道是否有可能重构扩展方法,所以可以像这样调用它...

qwerty.ThrowIfArgumentIsNull();
Run Code Online (Sandbox Code Playgroud)

它会自动确定变量的名称是'qwerty',因此将其用作ArgumentNullException的值.

可能?我假设反思可以做到这一点?

.net extension-methods

27
推荐指数
1
解决办法
2306
查看次数

标签 统计

.net ×1

extension-methods ×1