0

If I have an attribute before a method definition in C#, is this attribute always executed before the method starts running? I've been researching for the past hour but couldn't find any concrete information on this.

daremkd
  • 8,244
  • 6
  • 40
  • 66
  • Attributes an sich don't actually do anything -- they're just metadata. It depends on what you (or a framework) are doing with the attribute. – Jeroen Vannevel May 07 '16 at 19:40
  • @daremkd You mean the Attribute's contructor? http://stackoverflow.com/questions/1168535/when-is-a-custom-attributes-constructor-run – Manfred Radlwimmer May 07 '16 at 19:42

1 Answers1

2

If I have an attribute before a method definition in C#, is this attribute always executed before the method starts running?

No, it's not guaranteed at all. In fact, the usual behavior of the runtime seems to be that the attribute's constructor is run only when the attribute is requested (via reflection on the decorated member). As far as I can tell, only the attributes of the Main method are actually run without manual reflection.

Theodoros Chatzigiannakis
  • 28,773
  • 8
  • 68
  • 104