Why can you read an attribute placed on a const using reflection in C#? -
i playing around reflection , accident realized place custom field attribute on const class variable, (using reflection) read class' fields, find const attribute , perform actions. working fine.
i curious why works fine. unless mis-understood how consts work, thought constants "compiled out" , references constant became constant's actual value after compiling. if case, why can reflection still see const values?
all references const
compiled away - not const
declaration itself. const
declarations emitted part of il compiler.
here's example (notice il retains const
field).
c#:
class foo { const int = 0; }
il:
.class private auto ansi beforefieldinit foo extends [mscorlib]system.object { .method public hidebysig specialname rtspecialname instance void .ctor() cil managed { } .field private static literal int32 = int32(0) }
Comments
Post a Comment