using System; |
using System.Collections.Generic; |
using System.Linq; |
using System.Text; |
namespace LookUpGetAndSet |
{ |
class Program |
{ |
int age; |
public int Age |
{ |
get |
{ |
return this.age; |
} |
set |
{ |
age = value; |
} |
} |
static void Main(string[] args) |
{ |
Program p = new Program(); |
Console.WriteLine("please enter integer age:"); |
string age = Console.ReadLine(); |
try |
{ |
p.Age = Convert.ToInt32(age); |
Console.WriteLine(p.Age); |
Console.ReadLine(); |
} |
catch(Exception e) |
{ |
Console.WriteLine("you input the wrong age,please enter an integer number!"); |
} |
} |
} |
} |
反编译后,总体情况如下图所示:
查看Age属性,il代码如下:
.property instance int32 Age() |
{ |
.get instance int32 LookUpGetAndSet.Program::get_Age() |
.set instance void LookUpGetAndSet.Program::set_Age(int32) |
} // end of property Program::Age |
从上面可以看出来,属性中,get关联了get_Age()方法,set关联了set_Age(int32)这个方法。下面我们来看看生成的这两个方法:
get_Age()方法:
.method public hidebysig specialname instance int32 |
get_Age() cil managed |
{ |
// Code size 12 (0xc) |
.maxstack 1 |
.locals init ([0] int32 CS$1$0000) |
IL_0000: nop |
IL_0001: ldarg.0 |
IL_0002: ldfld int32 LookUpGetAndSet.Program::age |
IL_0007: stloc.0 |
IL_0008: br.s IL_000a |
IL_000a: ldloc.0 |
IL_000b: ret |
} // end of method Program::get_Age |
set_Age(int32)方法:
.method public hidebysig specialname instance void |
set_Age(int32 'value') cil managed |
{ |
// Code size 9 (0x9) |
.maxstack 8 |
IL_0000: nop |
IL_0001: ldarg.0 |
IL_0002: ldarg.1 |
IL_0003: stfld int32 LookUpGetAndSet.Program::age |
IL_0008: ret |
} // end of method Program::set_Age |