【发布时间】:2015-02-01 02:31:16
【问题描述】:
public sealed class test<T> : IEnumerable<T> where T : new()
{
public sealed class Ignore : Attribute { }
}
public class test_2
{
[ ???? ]
string helllo;
}
有没有办法从test<T> 类之外访问属性?
我好像找不到。
【问题讨论】:
-
我相信你的类需要被称为 IgnoreAttribute 才能正常工作。
-
[test<sometype>.Ignore]。您必须为T指定一个类型。这是因为类型参数T可用于嵌套类型,即使您的属性不使用它。 -
这使用起来很尴尬——为什么在泛型类中有非泛型属性?无论如何,将属性放在类中并不是一个好习惯
-
但是我不能为 T 指定一个类型,intellisense 没有提出一些建议,编译器也不接受这些@vcsjones
-
任何嵌套在泛型类中的类实际上代表每个不同的泛型参数的不同类。所以
test<sometype>.Ignore与test<othertype>.Ignore是不同的类型。但是,正如 bbeda 在下面回答的那样,C# 不支持泛型属性,因此您不能在泛型中嵌入属性类并将其用作属性。
标签: c# c#-4.0 generics custom-attributes