C 各種反射動態賦值取值方法

2021-06-08 22:44:51 字數 4101 閱讀 9970

using system;

using system.collections.generic;

using system.linq;

using system.web;

using system.web.ui;

using system.web.ui.webcontrols;

using system.data;

using kaicommon;

using system.reflection;

using kaicommon.orm;

using system.linq.expressions;

using system.reflection.emit;

public delegate void propertysetter(t value);

public delegate t propertygetter();

public partial class tools_testgrid : system.web.ui.page

public void buildgetmethod(testdata td)

#endregion

#region 表示式樹實現

funclmdgetprop; //func

public void lmdget(type entitytype, string propname)

actionlmdsetprop;

public void lmdset(type entitytype, string propname)

#endregion

#region emit動態方法實現

public delegate void setvaluedelegatehandler(testdata entity, object value);

public setvaluedelegatehandler emitsetvalue;

public void buildemitmethod(type entitytype, string propertyname)

, entitytype.module);

// 獲取動態函式的 il 生成器

var il = method.getilgenerator();

// 建立乙個本地變數,主要用於 object type to propety type

var local = il.declarelocal(para.parametertype, true);

// 載入第 2 個引數【(t owner, object value)】的 value

il.emit(opcodes.ldarg_1);

if (para.parametertype.isvaluetype)

else

il.emit(opcodes.stloc, local);// 將上面的拆箱或轉換,賦值到本地變數,現在這個本地變數是乙個與目標函式相同資料型別的字段了。

il.emit(opcodes.ldarg_0); // 載入第乙個引數 owner

il.emit(opcodes.ldloc, local);// 載入本地引數

il.emitcall(opcodes.callvirt, callmethod, null);//呼叫函式

il.emit(opcodes.ret); // 返回

/* 生成的動態函式類似:

* void emitcallable(t owner, object value)

* */

emitsetvalue = method.createdelegate(typeof(setvaluedelegatehandler)) as setvaluedelegatehandler;

}#endregion

protected void page_load(object sender, eventargs e)

timespan ts = datetime.now - time;

this.response.write("基本方法:" + ts.totalmilliseconds + "

");//反射方法

type type = d.gettype();

propertyinfo pi = type.getproperty("name");

time = datetime.now;

for (int i = 0; i < max; i++)

ts = datetime.now - time;

this.response.write("反射方法:" + ts.totalmilliseconds + "

");//dynamic動態型別方法

dynamic dobj = activator.createinstance();

time = datetime.now;

for (int i = 0; i < max; i++)

ts = datetime.now - time;

this.response.write("dynamic動態型別方法:" + ts.totalmilliseconds + "

");//泛型委託賦值方法

d.name = -1;

buildsetmethod(d);

time = datetime.now;

for (int i = 0; i < max; i++)

ts = datetime.now - time;

this.response.write("泛型委託賦值方法:" + ts.totalmilliseconds + "

");this.response.write("v:" + d.name + "

");//泛型委託取值方法

d.name = -1;

buildgetmethod(d);

time = datetime.now;

for (int i = 0; i < max; i++)

ts = datetime.now - time;

this.response.write("泛型委託取值方法:" + ts.totalmilliseconds + "

");this.response.write("v:" + d.name + "

");//表示式樹賦值方法

d.name = -1;

lmdset(typeof(testdata), "name");

time = datetime.now;

for (int i = 0; i < max; i++)

ts = datetime.now - time;

this.response.write("表示式樹賦值方法:" + ts.totalmilliseconds + "

");this.response.write("v:" + d.name + "

");//表示式樹取值方法

d.name = -132;

this.lmdget(typeof(testdata), "name");

time = datetime.now;

for (int i = 0; i < max; i++)

ts = datetime.now - time;

this.response.write("表示式樹取值方法:" + ts.totalmilliseconds + "

");this.response.write("v:" + this.lmdgetprop(d) + "

");//emit動態方法賦值

d.name = -1;

this.buildemitmethod(d.gettype(), "name");

time = datetime.now;

for (int i = 0; i < max; i++)

ts = datetime.now - time;

this.response.write("emit動態方法:" + ts.totalmilliseconds + "

");this.response.write("v:" + d.name + "

");//testdatabind();}}

}public class testdata

}

C 反射賦值

tb projects model new tb projects model.ownerid 123 string fieldname ownerid 欄位名稱 string value 333 type t model.gettype var p t.getproperty fieldname ...

Python中的取值賦值方法

class people object def init self,name self.name name def getname self return self.name def setname self,newname if len newname 5 self.name newname el...

jQuery 取值 賦值的基本方法整理

獲得text.areatext的值 var textval text id attr value 或者 var textval text id val 獲取單選按鈕的值 var valradio input type radio checked val 獲取一組名為 items 的radio被選中項...