C 反射取得方法 屬性 變數

2021-07-07 09:37:58 字數 3306 閱讀 9544

程式結構:

學生字典類(s0001):

using system;

using system.collections.generic;

using system.linq;

using system.text;

using system.data;

namespace dictionary.class.s0001

set 

}///

///建構函式

///public studentdict()

//////

根據序號查詢姓名

//////

///public

string getstunamebycode(string strcode)}}

學生字典類:(s0002)

using system;

using system.collections.generic;

using system.linq;

using system.text;

using system.data;

namespace dictionary.class.s0002

set 

}///

///建構函式

///public studentdict()

//////

根據序號查詢姓名

//////

///public

string getstunamebycode(string strcode)}} 

programe主類:

void main(string args)

catch (exception ex)

trycatch (exception ex)

foreach (fieldinfo field in type.getfields())

-> 姓名:

", key, dict[key]);}}

}catch (exception ex)

}methodinfo method = type.getmethod("

getstunamebycode

");string strstuname = (string)method.invoke(obj, new

string );

console.writeline("

\t\t學號【】 的學生姓名為:

","02

",strstuname);

console.readline();}}

} c#中訪問私有成員--反射

首先我必須承認訪問乙個類的私有成員不是什麼好做法。大家也都知道私有成員在外部是不能被訪問的。而乙個類中會存在很多私有成員:如私有字段、私有屬性、私有方法。對於私有成員訪問,可以套用下面這種非常好的方式去解決。

private string name;public string name set }

但是有時候,源**是別人的,你就不能修改源**,只提供給你dll。或者你去維護別人的**,源**卻有丟失。這樣的情況如果你想知道私有成員的值,甚至去想直接呼叫類裡面的私有方法。那怎麼辦呢?其實在.net中訪問私有成員不是很難,這篇文章提供幾個簡單的方法讓你如願以償。

為了讓**用起來優雅,使用擴充套件方法去實現。

1、得到私有欄位的值:

public static t getprivatefield(this object instance, string fieldname)

2、得到私有屬性的值:

public static t getprivateproperty(this object instance, string propertyname)

3、設定私有成員的值:

public static void setprivatefield(this object instance, string fieldname, object value) 

4、設定私有屬性的值:

public static void setprivateproperty(this object instance, string propertyname, object value) 

5、呼叫私有方法:

public static t callprivatemethod(this object instance, string name, params object param)

測試:

下面我們使用乙個測試類,進行測試。新建乙個類庫專案,測試的類**如下:

public class testclass private int privatefield1; private int privatefield2; private string privatefielda private string privatefieldb private int add() private string join() }

將上面類庫的dll引入控制台專案中。使用下面**去使用這個類的私有成員:

testclass obj = new testclass();system.console.writeline("私有字段");system.console.writeline(obj.getprivatefield("privatefield1"));system.console.writeline(obj.getprivatefield("privatefield2"));system.console.writeline("私有屬性");system.console.writeline(obj.getprivateproperty("privatefielda"));system.console.writeline(obj.getprivateproperty("privatefieldb"));system.console.writeline("私有方法");system.console.writeline(obj.callprivatemethod("add",null));system.console.writeline(obj.callprivatemethod("join", null));system.console.writeline("修改私有屬性");obj.setprivateproperty("privatefielda", "hello");obj.setprivateproperty("privatefieldb", "world");system.console.writeline(obj.callprivatemethod("join", null));system.console.read();

反射 通過反射呼叫類中指定方法 屬性

一 呼叫指定方法 通過反射,呼叫類中的方法,通過method類完成。步驟 1 通過class類的getmethod string name,class.parametertypes 方法取得乙個method物件,並設定此方法操作時所需要的引數型別。2 使用object invoke object o...

反射 通過反射呼叫類中指定方法 屬性

通過反射,呼叫類中的方法,通過method類完成。步驟 1 通過class類的getmethod string name,class parametertypes 方法取得乙個method物件,並設定此方法操作時所需要的引數型別。2 使用object invoke object obj,object...

C 方法,屬性,和事件

1.7.3 方法 方法是乙個執行可以由對像或類完成的計算或行為的成員。方法有乙個形式引數列表 可能為空 乙個返回數值 或void 並且可以是靜態也可以是非靜態。靜態方法要通過類來訪問。非靜態方法,也稱為例項方法,通過類的例項來訪問。例子 using system public class stack...