日韩久久久精品,亚洲精品久久久久久久久久久,亚洲欧美一区二区三区国产精品 ,一区二区福利

Mcad學習筆記之通過反射調用類的方法,屬性,字段

系統 1923 0

Type是System.Reflection功能的根 (Root),也是存取Metadata的主要方法.
使用Type的成員可以取得相關資訊,例如建構函式(Constructor),方法,字段,屬性和類別的事件,以及模組和部署類別的組件(Assembly).

3種取得Type的方法:
1.靜態方法
Type.GetType()
2.運算符
typeof()
3.實例的方法GetType
Employee e=new Employee();
e.GetType()

在一般情況下我們調用的方法並傳遞給它們的參數,某些情況下可能希望根據用戶操作動態調用方法.
通過Reflection命名空間
方法1是使用Type對象上的InvokeMember方法
方法2是使用MethodInfo對象上的Invoke方法

example:
先定義類Employee
其中有靜態屬性Data
實例屬性Name,ID
2個索引器

???? /// ? <summary>
????
/// ?自定義類
????
/// ? </summary>
???? public ? class ?Employee
????{
????????
string ?name;
????????
int ?id;

????????ArrayList?list;?

????????
static ? int ?data;
????????
????????
// instance?.ctor()
???????? public ?Employee(? int ?id,?String?name?)?
????????{
????????????
this .name? = ?name;
????????????
this .id? = ?id;

????????????list
= new ?ArrayList();

????????????
this .list.Add(? " 001 " ?);
????????????
this .list.Add(? " 002 " ?);
????????}

????????
// static?.ctor()
???????? static ?Employee()
????????{
????????????data
= 119 ;
????????}

????????
public ? override ? string ?ToString?()
????????{
????????????
return ? " Id= " + ?id.ToString()? + ? " ,?Name= " ? + ?name;
????????}

????????
// instance?Method?"add"
???????? public ? string ?add(? string ?key1, string ?key2?)
????????{
????????????
string ?result = key1 + key2;
????????????
return ?result;
????????}
????????
????????
// static?Method?"add"
???????? public ? static ? string ?add(? string ?key1, string ?key2, string ?key3?)
????????{
????????????
return ?key1 + key2 + key3;
????????}

????????
public ? string ?Name?
????????{
????????????
get ?
????????????{?
????????????????
return ??name;?
????????????}
????????????
set ?
????????????{?
????????????????name?
= ?value;?
????????????}
????????}

????????
public ? int ?ID?
????????{
????????????
get ?
????????????{?
????????????????
return ??id;?
????????????}
????????????
set ?
????????????{?
????????????????id?
= ?value;?
????????????}
????????}

????????
public ? static ? int ?Data
????????{
????????????
get
????????????{
????????????????
return ?data;
????????????}
????????????
set
????????????{
????????????????data
= value;
????????????}
????????}

????????
/// ? <summary>
????????
/// ?by?index
????????
/// ? </summary>
???????? public ? string ? this ?[? int ?index?]
????????{
????????????
get
????????????{
????????????????
return ?list[index].ToString();
????????????}
????????????
set
????????????{
????????????????list[index]
= value;
????????????}
????????}

????????
/// ? <summary>
????????
/// ?by?value
????????
/// ? </summary>
???????? public ? string ? this ?[? string ?values]
????????{
????????????
set
????????????{
????????????????
this [list.IndexOf(values)] = value;
????????????}
????????}

????}

動態調用:

定義變量

???????????? string ?result = String.Empty;
????????????
int ?i;

????????????Type?t
= typeof (?Employee?);
????????????
????????????Employee?e
= new ?Employee( 1000 , " no?1000 " );



方法1是使用Type對象上的InvokeMember方法:
先動態調用類Employee的實例方法ToString
InvokeMember方法的第一個參數是要調用的方法名稱
第2個參數是位枚舉,代表搜尋的方式
第四個參數是要調用的對象,要是靜態的就用null
第五個參數是要傳送給ToString的數值,由於ToString方法是無參方法,這裡我們送一個空數組new object[]{}

1 ? ???????????? // call?instance?Method?"ToString"
2 ? ????????????result = ( typeof (Employee).InvokeMember( " ToString " ,
3 ? ????????????????????????????????????????????????????BindingFlags.InvokeMethod,????
4 ? ???????????????????????????????????????????????????? null ,
5 ? ????????????????????????????????????????????????????e,????
6 ? ???????????????????????????????????????????????????? new ? object []{})).ToString();
7 ? ????????????Console.WriteLine( " instance?Method?'ToString'?result={0} " ,?result?);
8 ?


再動態調用類Employee的實例方法add,靜態方法add
注意InvokeMember方法的第5個參數代表的是要傳送給add方法的數值
第8個參數代表的是add方法的參數名稱

???????????? // call?instance?Method?"add"
????????????result = typeof (Employee).InvokeMember( " add " ,
????????????????????????????????????????????????BindingFlags.InvokeMethod,
????????????????????????????????????????????????
null ,
????????????????????????????????????????????????e,
????????????????????????????????????????????????
new ? object []{ " o1 " , " o2 " },
????????????????????????????????????????????????
null ,
????????????????????????????????????????????????
null ,
????????????????????????????????????????????????
new ? string []{ " key1 " , " key2 " }).ToString();
????????????Console.WriteLine(
" instance?Method?'add'?result={0} " ,?result?);

????????????
// call?static?Method?"add"
????????????result = typeof (Employee).InvokeMember( " add " ,
????????????????????????????????????????????????BindingFlags.InvokeMethod?
| ?BindingFlags.Static? | ?BindingFlags.Public?,
????????????????????????????????????????????????
null ,
????????????????????????????????????????????????
null ,
????????????????????????????????????????????????
new ? object []{ " o1 " , " o2 " , " o3 " },
????????????????????????????????????????????????
null ,
????????????????????????????????????????????????
null ,
????????????????????????????????????????????????
new ? string []{ " key1 " , " key2 " , " key3 " }).ToString();
????????????Console.WriteLine(
" static?Method?'add'?result={0} " ,result?);
????????????Console.WriteLine();


再修改靜態屬性Data,把它從119修改為911

?1 ? ???????????? // call?static?Property
?2 ? ????????????i = ( int ) typeof (Employee).InvokeMember( " Data " ,
?3 ? ????????????????????????????????????????????????BindingFlags.GetProperty? | ?BindingFlags.Public? | ?BindingFlags.Static,
?4 ? ???????????????????????????????????????????????? null ,
?5 ? ???????????????????????????????????????????????? null ,
?6 ? ???????????????????????????????????????????????? new ? object []{});
?7 ? ????????????Console.WriteLine( " static?Property?'Data'={0} " ,?i?);
?8 ?
?9 ? ???????????? // update?static?Property
10 ? ???????????? typeof (Employee).InvokeMember( " data " ,
11 ? ????????????????????????????????????????????BindingFlags.SetField? | ?BindingFlags.NonPublic? | ?BindingFlags.Static,
12 ? ???????????????????????????????????????????? null ,
13 ? ???????????????????????????????????????????? null ,
14 ? ???????????????????????????????????????????? new ? object []{ 911 });
15 ? ????????????Console.WriteLine( " update?static?Property? " );
16 ?
17 ? ???????????? // call?static?Property
18 ? ????????????i = ( int ) typeof (Employee).InvokeMember( " Data " ,
19 ? ????????????????????????????????????????????????BindingFlags.GetProperty? | ?BindingFlags.Public? | ?BindingFlags.Static,
20 ? ???????????????????????????????????????????????? null ,
21 ? ???????????????????????????????????????????????? null ,
22 ? ???????????????????????????????????????????????? new ? object []{});
23 ? ????????????Console.WriteLine(? " again?call?static?Property?'Data'={0} " ,i?);
24 ? ????????????Console.WriteLine();
25 ?


再修改實例屬性Name,把它從no 1000修改為w

?1 ? ???????????? // read?instance?Property
?2 ? ????????????result = typeof (Employee).InvokeMember( " Name " ,
?3 ? ????????????????????????????????????????????????BindingFlags.GetProperty?,
?4 ? ???????????????????????????????????????????????? null ,????
?5 ? ????????????????????????????????????????????????e,????
?6 ? ???????????????????????????????????????????????? new ? object []{}).ToString();
?7 ? ????????????Console.WriteLine(? " instance?Property?'Name'={0} " ,?result?);
?8 ?
?9 ? ???????????? // update?instance?property
10 ? ???????????? typeof (Employee).InvokeMember( " name " ,?
11 ? ????????????????????????????????????????????BindingFlags.SetField? | ?BindingFlags.NonPublic?? | ?BindingFlags.Instance????,
12 ? ???????????????????????????????????????????? null ,
13 ? ????????????????????????????????????????????e,
14 ? ???????????????????????????????????????????? new ? object []{ " w " });
15 ? ????????????Console.WriteLine( " update?instance?property? " );
16 ? ????????????
17 ? ???????????? // again?call?read?instance?Property
18 ? ????????????result = typeof (Employee).InvokeMember( " Name " ,
19 ? ????????????????????????????????????????????????????BindingFlags.GetProperty?,
20 ? ???????????????????????????????????????????????????? null ,????
21 ? ????????????????????????????????????????????????????e,
22 ? ???????????????????????????????????????????????????? new ? object []{}).ToString();
23 ? ????????????Console.WriteLine(? " again?call?instance?Property?'Name'={0} " ,?result?);
24 ? ????????????Console.WriteLine();
25 ?


再修改索引器,把索引器的第2個(index[1])內容修改為222
注意修改索引器的InvokeMember方法,第5個參數的數組new object[]{"002","222"}
將要設置元素的索引值放在對象數組的第一個元素中,將要設置的值作為第二個元素

?1 ? ???????????? // call?index[1]?
?2 ? ????????????result = typeof (Employee).InvokeMember( " Item " ,
?3 ? ????????????????????????????????????????????????????BindingFlags.GetProperty,
?4 ? ???????????????????????????????????????????????????? null ,
?5 ? ????????????????????????????????????????????????????e,
?6 ? ???????????????????????????????????????????????????? new ? object []{ 1 }).ToString()?;
?7 ? ????????????Console.WriteLine( " index[1]={0} " ,?result);
?8 ?
?9 ? ???????????? // update??index[1]?
10 ? ???????????? typeof (Employee).InvokeMember( " Item " ,
11 ? ????????????????????????????????????????????BindingFlags.SetProperty,
12 ? ???????????????????????????????????????????? null ,????
13 ? ????????????????????????????????????????????e,
14 ? ???????????????????????????????????????????? new ? object []{ " 002 " , " 222 " });
15 ? ????????????Console.WriteLine( " update??index[1]? " );
16 ?
17 ? ???????????? // again?call?index[1]
18 ? ????????????result = typeof (Employee).InvokeMember( " Item " ,
19 ? ????????????????????????????????????????????????BindingFlags.GetProperty,
20 ? ???????????????????????????????????????????????? null ,
21 ? ????????????????????????????????????????????????e,
22 ? ???????????????????????????????????????????????? new ? object []{ 1 }).ToString()?;
23 ?
24 ? ????????????Console.WriteLine( " again?call?index[1]={0} " ,?result);
25 ? ????????????Console.WriteLine();
26 ?


調用構造器
InvokeMember方法的第1個參數為空字符""

1 ? ???????????? // call?instance?.ctor()
2 ? ????????????Employee?employee = (Employee) typeof (Employee).InvokeMember( "" ,
3 ? ????????????????????????????????????????????????????????????????????????BindingFlags.CreateInstance,
4 ? ???????????????????????????????????????????????????????????????????????? null ,
5 ? ???????????????????????????????????????????????????????????????????????? null ,
6 ? ???????????????????????????

Mcad學習筆記之通過反射調用類的方法,屬性,字段,索引器(2種方法)


更多文章、技術交流、商務合作、聯系博主

微信掃碼或搜索:z360901061

微信掃一掃加我為好友

QQ號聯系: 360901061

您的支持是博主寫作最大的動力,如果您喜歡我的文章,感覺我的文章對您有幫助,請用微信掃描下面二維碼支持博主2元、5元、10元、20元等您想捐的金額吧,狠狠點擊下面給點支持吧,站長非常感激您!手機微信長按不能支付解決辦法:請將微信支付二維碼保存到相冊,切換到微信,然后點擊微信右上角掃一掃功能,選擇支付二維碼完成支付。

【本文對您有幫助就好】

您的支持是博主寫作最大的動力,如果您喜歡我的文章,感覺我的文章對您有幫助,請用微信掃描上面二維碼支持博主2元、5元、10元、自定義金額等您想捐的金額吧,站長會非常 感謝您的哦!!!

發表我的評論
最新評論 總共0條評論
主站蜘蛛池模板: 山西省| 安庆市| 禄丰县| 汉沽区| 神农架林区| 金门县| 南宫市| 长武县| 商洛市| 茶陵县| 漠河县| 定日县| 昌都县| 芜湖县| 巴林右旗| 广灵县| 鹤山市| 萍乡市| 凌源市| 四川省| 鸡西市| 潼关县| 陵水| 成武县| 固原市| 乌海市| 紫金县| 武定县| 卓资县| 泗洪县| 确山县| 凤山县| 泸西县| 炎陵县| 洛阳市| 满洲里市| 台北县| 祁连县| 剑阁县| 东安县| 张家港市|