.NET开发

本类阅读TOP10

·NHibernate快速指南(翻译)
·vs.net 2005中文版下载地址收藏
·【小技巧】一个判断session是否过期的小技巧
·VB/ASP 调用 SQL Server 的存储过程
·?dos下编译.net程序找不到csc.exe文件
·通过Web Services上传和下载文件
·学习笔记(补)《.NET框架程序设计(修订版)》--目录
·VB.NET实现DirectDraw9 (2) 动画
·VB.NET实现DirectDraw9 (1) 托管的DDraw
·建站框架规范书之——文件命名

分类导航
VC语言Delphi
VB语言ASP
PerlJava
Script数据库
其他语言游戏开发
文件格式网站制作
软件工程.NET开发
IL系列文章之五:Property in IL

作者:未知 来源:月光软件站 加入时间:2005-2-28 月光软件站

IL系列文章之五:

Property in IL

C#Property(属性)被称为智能字段,是一个很有趣的东西。为什么称它是智能字段呢?因为我们可以像使用字段那样来使用它。

我们先来回顾一下C++或者其他传统的面向对象语言。在Class中我们会定义一些data field,我们在另一个Class中可能需要访问它。但是你能仅仅将其简单的申明为Public吗?答案肯定是不能!我们得把它封装起来。实际上,最常用的方法就是gettersetter,在我们读取或者设置它的时候要对其进行一些必要的处理,以保证我们Class的安全。OK!现在C#出现了,Property代替了gettersetter,它的目的仅仅为了是程序的使用者感觉更加自然一些。直接使用一个简单的类成员访问方法(Class.datafield)比使用一个函数调用更加自然,还能隐藏一些后台的技术细节。但是一个简单的访问(Class.Property)是怎样产生如同函数调用的神奇效果呢?让我来告诉你,其实它就是一个gettersetter。不信啊?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神奇的效果其实就是来源于和传统的gettersetter一样的方法。在我们将C#程序编译为IL的时候,Property就被编译为以get_set_为前缀的函数。在运行时IL也是通过调用这两个来实现getset中的定义。既然这样,那我们就在C#中显式的调用windfast.get_Age()这个函数吧,你试试……这是不可能的!呵呵。

大家注意看了,IL中有这样的一段代码“.property instance int32 Age(){……}”,这是用来定义Property的。既然C#在编译的过程中就直接将Property作为gettersetter来处理了,那何以还要定义它呢?这样做目的是为了能够使用metadata来记录关于Property的信息,并且可以在运行时通过Reflection来获取这个信息。




相关文章

相关软件