CA1004: Generic methods should provide type parameterCA1004和异常/错误处理援助
public static void IfNullAndNullsAreIllegalThenThrow<T>(object value)
{
if (value == null && !(default(T) == null))
throw new ArgumentException("Nulls are not allowed for this object.");
}
我发现这个方法在网上,这是非常有用的是诚实的。但是,它违反了CA1004规则。我不确定是否有更好的方法来设计方法而不违反规则。
使用范例:
public class SomeClass<T>
{
public void SomeMethod(object obj)
{
// Ensure the actual object is not null if it shouldn't be.
ThrowHelper.IfNullAndNullsAreIllegalThenThrow<T>(obj);
// ...
}
}
但是,如果您想在一个或多个程序集中使用这些辅助方法,这不是解决方案。所以你需要复制代码。 –