Are attributes instantiated at the same time as instances of the class to which they are applied?
Asked
Active
Viewed 4,657 times
2 Answers
30
Attributes are loaded when the Type.GetCustomAttributes()
is called. If you just instantiate the class or even get the type, the attributes are not yet loaded.
See this example:
class Program
{
static void Main()
{
Console.WriteLine("Calling AttributeLoadTest.TestStatic():");
AttributeLoadTest.TestStatic();
Console.WriteLine("Loading class");
var x = new AttributeLoadTest();
Console.WriteLine("Loading type..");
var t = typeof (AttributeLoadTest);
Console.WriteLine("Calling GetCustomAttributes()");
var ats = t.GetCustomAttributes(false);
}
}
[ConsoleTest]
class AttributeLoadTest
{
public static void TestStatic()
{
}
}
[AttributeUsage(AttributeTargets.Class)]
class ConsoleTestAttribute : Attribute
{
public ConsoleTestAttribute()
{
Console.WriteLine("ConsoleTestAttribute Constructor");
}
}
This prints out:
Calling AttributeLoadTest.TestStatic():
Loading class
Loading type..
Calling GetCustomAttributes()
ConsoleTestAttribute Constructor
So calling a static method, instantiating the class, getting the type, does not instantiate the attribute. Calling GetCustomAttributes()
does.

Community
- 1
- 1

Samuel Neff
- 73,278
- 17
- 138
- 182
-
I've just come across this behavior myself, and am wondering what the rationale behind this is? Why would a type's attributes not be pre-loaded like the rest of a Type's object graph? – Sebastian Nemeth May 23 '13 at 03:28
-
1@Martaver, delayed loading is a very common practice in software development. It's generally more efficient to not load things until you need them. It's always more efficient if you usually never need them. – Samuel Neff May 28 '13 at 17:52
-
2@SamualNeff Thanks for replying, I understanding the purpose of lazy loading, but I was actually more interested in WHY attributes would need to be lazy loaded, as opposed to loaded at the same time as the parent Type. With a bit of digging I found the answer here: http://stackoverflow.com/a/417302/535930 – Sebastian Nemeth May 29 '13 at 06:34
-
Is this true? Is an attribute not bound to a type before or just after run time starts? I am new to .net however I noticed after inserting a breakpoint in my code it looks to me that the custom attribute class I have defined is getting instantiated before my any of my test code gets executed. – Stewart Aug 13 '15 at 00:24
-
1@Stewart, it's true, I didn't fake the console output. :-) I rechecked it now since this post is a few years old, and got the same results with .NET 4.0. – Samuel Neff Aug 13 '15 at 02:33
-
@SamuelNeff Humm could it be the JIT? Perhaps in my project that JIT is initializing the type at a different time. Still does not explain why I am seeing the constructor of my custom attribute class be called long before I call 'GetCustomAttributeData()'. Maybe I am calling some other method in my code that causes this to run but I could not possibly imagine where I am doing that. – Stewart Aug 13 '15 at 05:21
-
@Stewart, add code in your constructor to print out a stack trace. If you run the exact code above, do you get the same results posted or something different? – Samuel Neff Aug 13 '15 at 16:33
-2
No. Attributes are bound to the type and thus instantiated when the type is loaded.

JohnB
- 13,315
- 4
- 38
- 65
-
-
4No, attributes are not loaded when the type is loaded. They aren't loaded until they're actually needed. – Samuel Neff Aug 30 '12 at 12:33
-
Strange. Thank you for testing. Now I wonder whether (i) GetCustomAttributes generates new attribute objects every time when it is called and (ii) the attribute classes are loaded earlier. – JohnB Aug 30 '12 at 12:40
-
5@JohnB, a new instance is created every time you call `GetCustomAttributes()`. – Samuel Neff Aug 30 '12 at 12:50
-
2
-
@JohnB, yeah, I was surprised at the answer actually. Sometimes the only way to know for sure is to test. – Samuel Neff Aug 30 '12 at 16:43