2009-01-20 8 views

Répondre

2

Il n'existe vraiment pas de propriétés dans IL. Il n'y a que des champs et des méthodes. La construction de la propriété C# est convertie en méthodes get_PropertyName et set_PropertyName par le compilateur. Vous devez donc appeler ces méthodes pour accéder à la propriété.

Sample (debug) IL pour le code

var s = "hello world"; 
var i = s.Length; 

IL

.locals init ([0] string s, 
      [1] int32 i) 
    IL_0000: nop 
    IL_0001: ldstr  "hello world" 
    IL_0006: stloc.0 
    IL_0007: ldloc.0 
    IL_0008: callvirt instance int32 [mscorlib]System.String::get_Length() 
    IL_000d: stloc.1 

Comme vous pouvez le voir la propriété Length est accessible via l'appel à get_Length.

0

Je triché ... Je pris le code C# suivant et a pris un coup d'oeil dans ildasm/réflecteur

static void Main(string[] args) 
{ 
    string h = "hello world"; 
    int i = h.Length; 
} 

est équivalent à

.method private hidebysig static void Main(string[] args) cil managed 
{ 
    .entrypoint 
    .maxstack 1 
    .locals init (
     [0] string h, 
     [1] int32 i) 
    L_0000: nop 
    L_0001: ldstr "hello world" 
    L_0006: stloc.0 
    L_0007: ldloc.0 
    L_0008: callvirt instance int32 [mscorlib]System.String::get_Length() 
    L_000d: stloc.1 
    L_000e: ret 
} 
Questions connexes