Fork me on GitHub

.net知识和学习方法系列(五)关于C#的属性

一次教学,发现了属性的两个访问器其实是两个方法,于是,就做了个例子来证明一下,代码如下:
 1using System;
 2using System.Collections.Generic;
 3using System.Linq;
 4using System.Text;
 5using System.Reflection;
 6
 7namespace Demo
 8{
 9    class Program
10    {
11        static void Main(string[] args)
12        {
13            ClassA DX = new ClassA();
14            object[] CS1 = new object[0];
15            object[] CS2 = new object[1]"这里是参数" };
16            typeof(ClassA).GetMethod("get_SX").Invoke(DX,CS1);
17            typeof(ClassA).GetMethod("set_SX").Invoke(DX, CS2);
18        }

19    }

20
21    class ClassA
22    {
23        public   string SX
24        {
25            get
26            {
27                Console.WriteLine("属性的get");
28                return "True";              
29            }

30            set
31            {
32                Console.WriteLine("属性的set"+value);
33            }

34        }

35    }

36   }

37
在Main方法中,我们用反射来显式的调用属性的get 和set 对应的方法成功了!
posted @ 2008-01-20 13:10  桂素伟  阅读(448)  评论(0编辑  收藏  举报