一次教學,發現了屬性的兩個通路器其實是兩個方法,于是,就做了個例子來證明一下,代碼如下:
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 對應的方法成功了!
本文轉自桂素偉51CTO部落格,原文連結:http://blog.51cto.com/axzxs/149993 ,如需轉載請自行聯系原作者