c# – 当类没有实现必需的属性时,是否会抛出标准的.NET异常?

假设我想在调用一个没有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.
        }
    }
} 

谢谢.

最佳答案
我看到它的方式,你可以采取三种方式之一……
1)NotSupportedException
2)NotImplementedException3)You can make your own Exception type

转载注明原文:c# – 当类没有实现必需的属性时,是否会抛出标准的.NET异常? - 代码日志