a t*_*key 4 .net c# generics attributes
假设我想在调用一个没有required属性的类型的泛型方法时抛出一个新的异常.是否存在适合这种情况的.NET异常,或者更可能是适合自定义异常的祖先?
例如:
public static class ClassA
{
public static T DoSomething<T>(string p)
{
Type returnType = typeof(T);
object[] typeAttributes = returnType.GetCustomAttributes(typeof(SerializableAttribute), true);
if ((typeAttributes == null) || (typeAttributes.Length == 0))
{
// Is there an exception type in the framework that I should use/inherit from here?
throw new Exception("This class doesn't support blah blah blah");
// Maybe ArgumentException? That doesn't seem to fit right.
}
}
}
Run Code Online (Sandbox Code Playgroud)
谢谢.
Mua*_*Dib 10
我看到它的方式,你可以采用3种方式之一...
1) 2)3)你可以制作自己的异常类型NotSupportedException
NotImplementedException