Say you have a class declaration, e.g.:
class MyClass { int myInt=7; int myOtherInt; }
Now, is there a way in generic code, using reflection (or any other means, for that matter), that I can deduce that myInt has a default value assigned, whereas myOtherInt does not? Note the difference between being initialised with an explicit default value, and being left to it’s implicit default value (myOtherInt will be initialised to 0, by default).
From my own research it looks like there is no way to do this – but I thought I’d ask here before giving up.
[Edit]
Even with nullable and reference types I want to distingush between those that have been left as null, and those that have been explicitly initialised to null. This is so that I can say that fields with an initialiser are ‘optional’ and other fields are ‘mandatory’. At the moment I’m having to do this using attributes – which niggles me with their redundancy of information in this case.
I compiled your code and load it up in ILDASM and got this
Note the
ldc.i4.7andstfld int32 dummyCSharp.MyClass::myIntseems to be instructions to set the default values for the myInt field.So such assignment is actually compiled as an additional assignment statement in a constructor.
To detect such assignment, then you will need reflection to reflect on the IL of MyClass’s constructor method and look for
stfld(set fields?) commands.EDIT: If I add some assignment into the constructor explicitly:
When I load it up in ILDASM, I got this:
Note that the extra assigment on myOtherInt that I added was addded after a call the Object class’s constructor.
So there you have it,
Any assignment done before the call to Object class’s constructor in IL is a default value assignment.
Anything following it is a statement inside the class’s actual constructor code.
More extensive test should be done though.
p.s. that was fun 🙂