小编a t*_*key的帖子

当类没有实现必需的属性时,是否会抛出标准的.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.
        }
    }
} 
Run Code Online (Sandbox Code Playgroud)

谢谢.

.net c# generics attributes

4
推荐指数
1
解决办法
298
查看次数

标签 统计

.net ×1

attributes ×1

c# ×1

generics ×1