更新:2007 年 11 月
属性可以应用于泛型类型中,方式与应用于非泛型类型相同。有关应用属性的更多信息,请参见 属性(C# 编程指南)。
自定义属性只允许引用开放泛型类型(未提供类型参数的泛型类型)和封闭构造泛型类型(为所有类型参数提供参数)。
下面的示例使用此自定义属性:
| C# | |
|---|---|
class CustomAttribute : System.Attribute { public System.Object info; }  | |
属性可以引用开放式泛型类型:
| C# | |
|---|---|
public class GenericClass1<T> { } [CustomAttribute(info = typeof(GenericClass1<>))] class ClassA { }  | |
使用数目适当的若干个逗号指定多个类型参数。在此示例中,GenericClass2 有两个类型参数:
| C# | |
|---|---|
public class GenericClass2<T, U> { } [CustomAttribute(info = typeof(GenericClass2<,>))] class ClassB { }  | |
属性可以引用封闭式构造泛型类型:
| C# | |
|---|---|
public class GenericClass3<T, U, V> { } [CustomAttribute(info = typeof(GenericClass3<int, double, string>))] class ClassC { }  | |
引用泛型类型参数的属性将导致编译时错误:
| C# | |
|---|---|
//[CustomAttribute(info = typeof(GenericClass3<int, T, string>))] //Error class ClassD<T> { }  | |
不能从 
| C# | |
|---|---|
//public class CustomAtt<T> : System.Attribute {}  //Error
 | |
若要在运行时获得有关泛型类型或类型参数的信息,可以使用