.net - What is the compiler/CLR actually doing when attributes annotate a type/method/property? -


when annotate type or method or property attribute, compiler/clr/etc. doing you?

my guess is "injecting" methods, properties, etc. class definitions (or maybe object?, or?) , providing automatic behavior, sort of how when declare delegate terse:

public delegate void mysuperspecialdelegate(myawesomeclass myawesomeobject); 

you great automatic behavior "injected" compiled code (cil) you.

so reiterate question, happening automatically "behind scenes" when use attributes?

it depends on attribute.

"normal" attributes clscompliantattribute don't do @ run-time (unless try read them, in case they're instantiated); they're metadata describing data/code, that's used compiler and/or debugger programmer.

"special" attributes are, well, special. can change flags in code, cause clr behavior change in way, or cause other observable change; it's attribute-dependent. (e.g. fieldoffsetattribute can cause field layout change, observable run-time effect.)

an extreme example of change in behavior can seen proxyattribute, in case can pretty hijack entire code class (even constructor).

other examples of such "special" attributes include inattribute, outattribute, threadstaticattribute, mtathreadattribute, typeforwardedtoattribute (i think), comimportattribute, dllimportattribute, etc, etc... there's quite lot of them!


Comments

Popular posts from this blog

c# - How to set Z index when using WPF DrawingContext? -

razor - Is this a bug in WebMatrix PageData? -

visual c++ - Using relative values in array sorting ( asm ) -