I've found that attributes in C# seem to be lazily instantiated.
[A(123)]
class A : Attribute
{
public A(int b)
{
GetType().GetCustomAttributes(true);
}
}
In this example, creating a new A
instance causes a StackOverflowException
, but if I remove the call to GetCustomAttributes()
, then execution carries on normally.
Why is this? It makes more sense to me that attributes are properly initialized when the class they decorate is.