我有以下擴展方法:是否可以重構此擴展方法?
public static void ThrowIfArgumentIsNull<T>(this T value, string argument)
where T : class
{
if (value == null)
{
throw new ArgumentNullException(argument);
}
}
,這是它的使用的一個例子....
// Note: I've poorly named the argument, on purpose, for this question.
public void Save(Category qwerty)
{
qwerty.ThrowIfArgumentIsNull("qwerty");
....
}
工程100%細。
但是,我不喜歡我怎麼也得提供變量的名稱,只是爲了幫助我的異常信息。
我在想,如果有可能重構擴展方法,因此它可以被稱爲是這樣的...
qwerty.ThrowIfArgumentIsNull();
,它會自動計算出該變量的名稱是「QWERTY」,因此使用它作爲ArgumentNullException的值。
可能嗎?我假設反思可以做到這一點?
另請參閱http://stackoverflow.com/questions/869610/c-resolving-a-parameter-name-at-runtime/869629#869629 – 2009-12-09 13:34:16
檢查http://msmvps.com/blogs/jon_skeet/archive/ 2009/12/09/quot; magic-quot; null-argument-testing.aspx - 博客:) – 2009-12-09 18:10:16
我認爲這是一個非常簡單的問題非常困難的解決方案...如果你使用Visual Studio,你可以使用代碼片段使這非常容易;) – 2009-12-09 19:22:15