我发现 C# 中的属性似乎是延迟实例化的。
[A(123)]
class A : Attribute
{
public A(int b)
{
GetType().GetCustomAttributes(true);
}
}
在此示例中,创建一个新A
实例会导致 a StackOverflowException
,但如果我删除对GetCustomAttributes()
,则执行会正常进行。
为什么是这样?对我来说更有意义的是,当它们装饰的类是属性时,它们会被正确初始化。