C# 当类没有';不实现必需的属性?

C# 当类没有';不实现必需的属性?,c#,.net,generics,attributes,C#,.net,Generics,Attributes,假设我想在使用没有必需属性的类型调用泛型方法时抛出一个新异常。是否存在适合这种情况的.NET异常,或者更可能的是,是否存在适合自定义异常的祖先异常 例如: public static class ClassA { public static T DoSomething<T>(string p) { Type returnType = typeof(T); object[] typeAttributes = returnType.Get

假设我想在使用没有必需属性的类型调用泛型方法时抛出一个新异常。是否存在适合这种情况的.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.
        }
    }
} 
公共静态类ClassA
{
公共静态T剂量仪(字符串p)
{
类型returnType=类型(T);
object[]typeAttributes=returnType.GetCustomAttributes(typeof(SerializableAttribute),true);
如果((typeAttributes==null)| |(typeAttributes.Length==0))
{
//框架中是否存在我应该在此使用/继承的异常类型?
抛出新异常(“此类不支持诸如此类”);
//也许是例外?那似乎不合适。
}
}
} 

谢谢。

我知道我已经看到一些内置代码模板将NotImplemented异常作为占位符抛出,因此这可能是一个很好的开始。

在我看来,您可以选择以下三种方式之一。。。
1)

2)
3)我相信这是一个不错的选择。另外,和是特定于反射的。

NotSupportedException as NotImplemented从技术上讲是针对存根的,而不是这里发生的情况。不支持该类型


也就是说,我不知道为什么微软没有提供规范列表及其内部定义。这让人难以置信。

你为什么不花30秒的时间做你自己的呢?