Thursday, April 22, 2010

Organ Donors Bracelet

Overriding and Shadowing of methods in IT.

Right now I'm playing around with the

CIL opcodes

hiding under our good old C #. In trying to overload and hide (shadow) successively inherited two properties, I noticed that the compilo generated a series of metadata rather confusing. Consider the following example: public class BaseClass

{public int Prop1 {get; set;} public virtual int Prop2 {get; set;} public virtual int Prop3 {get; set;}} public class DerivClass: BaseClass {public int Prop1 {get; set;} public new int Prop2 {get; set;} public new virtual int Prop3 {get; set;}}
On the side of the base class are obtained on the following metadata getter
. method public instance int32 hidebysig specialname get_Prop1 () cil managed
virtual:

. method public virtual instance hidebysig specialname newSlot get_Prop2 int32 () cil managed joint notice the use of metadata and virtual newSlot, right after this I am going. side derived class is obtained that: not virtual:

. Method public instance int32 hidebysig specialname get_Prop1 () cil managed

new :
. Method public instance int32 hidebysig specialname get_Prop2 () cil managed virtual 
new:

. Method public virtual instance hidebysig specialname newSlot get_Prop3 int32 () cil managed

 Oulaaa ... That's where things get complicated! In the base class, as I noted earlier, the metadata used in conjunction compilo virtual and newSlot. Logically, one would have expected to see only the virtual. 
 In the derived class, shadowing the non-virtual public new int 

Prop2 {get; set;}

product metadata token strictly identical to a normal property, and the virtual shadowing IT produces an identical virtual property.

From these findings we can deduce that: newSlot token, not enough to say that this is a shadowing method (the getter non-virtual base class getter and the new derived class having the same metadata).
 
By cons, if a method contains the virtual token but not newSlot then is that it has hit on an overriding and no shadowing.
 
By inference, we can assume that if a method is not an overriding (newSlot the token is present), then there is shadowing when the signature [return type, name and method parameters] method also exists in the base class.
 

0 comments:

Post a Comment