IL 系列文章之五:
** Property in IL **
在 C# 中 Property (属性)被称为智能字段,是一个很有趣的东西。为什么称它是智能字段呢?因为我们可以像使用字段那样来使用它。
我们先来回顾一下 C++ 或者其他传统的面向对象语言。在 Class 中我们会定义一些 data field ,我们在另一个 Class 中可能需要访问它。但是你能仅仅将其简单的申明为 Public 吗?答案肯定是不能!我们得把它封装起来。实际上,最常用的方法就是 getter 和 setter ,在我们读取或者设置它的时候要对其进行一些必要的处理,以保证我们 Class 的安全。 OK !现在 C# 出现了, Property 代替了 getter 和 setter ,它的目的仅仅为了是程序的使用者感觉更加自然一些。直接使用一个简单的类成员访问方法( Class.datafield )比使用一个函数调用更加自然,还能隐藏一些后台的技术细节。但是一个简单的访问( Class.Property )是怎样产生如同函数调用的神奇效果呢?让我来告诉你,其实它就是一个 getter 和 setter 。不信啊? Property 怎么会是函数呢?那好,我们一起来看看。先看一个例子。
//PropertyTest.cs
using System;
class student
{
protected int age;
public int Age
{
get
{
Console.WriteLine("I am in get");
return age;
}
set
{
Console.WriteLine("I am in set");
age = value;
}
}
}
class app
{
public static void Main ()
{
student windfast = new student();
int myAge;
windfast.Age = 21;
myAge = windfast.Age;
}
}
这是一个很简单的例子,运行起来就会有两行输出。还有一个奇怪的东西不知道你注意到没有?就是 set 里面的 value ,这个 value 是怎么冒出来的?不是先申明它的类型就是可以用么?我们这就来分析它,还是老办法——反编译它。下面就是它的 IL 代码。(我写了这么几期文章,感觉越来越难给大家讲例子了, IL 代码越来越长。大家一看——这么长?!可能就不愿意往下看了。我想还是先把它看完吧。)
.assembly property{}
.class private auto ansi beforefieldinit student
extends [mscorlib]System.Object
{
.field family int32 age //data field “age”
.method public hidebysig specialname instance int32 //it’s a method!
get_Age() cil managed //a getter, return int32
{
.maxstack 1
.locals init (int32 V_0)
ldstr "I am in get"
call void [mscorlib]System.Console::WriteLine(string)
ldarg.0 // The ldarg.0 instruction loads the method's first argument
//(the hidden this pointer) onto the stack.
ldfld int32 student::age
stloc.0 //let the local variable equaled student::age
br.s IL_0013
IL_0013: ldloc.0 // load the return value onto the stack
ret
} // end of method student::get_Age
.method public hidebysig specialname instance void
set_Age(int32 'value') cil managed // there is parameter named 'value'
{
.maxstack 2
ldstr "I am in set"
call void [mscorlib]System.Console::WriteLine(string)
ldarg.0
ldarg.1
stfld int32 student::age // student::age = value
ret
} // end of method student::set_Age
.method public hidebysig specialname rtspecialname
instance void .ctor() cil managed //the constructor of this class
{
.maxstack 1
ldarg.0
call instance void [mscorlib]System.Object::.ctor()
//call the constructor of parent
ret
} // end of method student::.ctor
.property instance int32 Age() //the getter and setter are defined here
{
.get instance int32 student::get_Age() //be care of the return type
.set instance void student::set_Age(int32)
} // end of property student::Age
} // end of class student
.class private auto ansi beforefieldinit app
extends [mscorlib]System.Object
{
.method public hidebysig static void Main () cil managed
{
.entrypoint
.maxstack 2
.locals init (class student windfast, int32 myAge)
newobj instance void student::.ctor()
stloc windfast // student windfast = new student();
ldloc windfast
ldc.i4.s 21
callvirt instance void student::set_Age(int32) //call the setter
ldloc windfast
callvirt instance int32 student::get_Age()
stloc myAge
ret
} // end of method app::Main
} // end of class app
现在相信了嘛, Property 神奇的效果其实就是来源于和传统的 getter 和 setter 一样的方法。在我们将 C# 程序编译为 IL 的时候, Property 就被编译为以 get_ 和 set_ 为前缀的函数。在运行时 IL 也是通过调用这两个来实现 get 和 set 中的定义。既然这样,那我们就在 C# 中显式的调用 windfast.get_Age() 这个函数吧,你试试……这是不可能的!呵呵。
大家注意看了, IL 中有这样的一段代码“ .property instance int32 Age(){ …… } ”,这是用来定义 Property 的。既然 C# 在编译的过程中就直接将 Property 作为 getter 和 setter 来处理了,那何以还要定义它呢?这样做目的是为了能够使用 metadata 来记录关于 Property 的信息,并且可以在运行时通过 Reflection 来获取这个信息。