C 反射特性 一)

2022-02-14 02:10:13 字數 3144 閱讀 4743

using system;

using system.collections.generic;

using system.linq;

using system.text;

using system.reflection;

namespace reflectionexam

set

}private string color;

public string color

set

}public pepper(string strname, string col)

public void printdatas()

private string getmessage()

public static void showstaticmessage()

public int add(int n, int m)

public void showmsg(string str,string msg)

}class program

}public static void paramstest( string names)

}delegate void mymessage(string msg);

event mymessage mymsg;

delegate void showmessage(string name, string color);

public void show(string msg)

static void main(string args)

;// object pepperobject = activator.createinstance(typepepper, conparams);

// methodinfo method = typepepper.getmethod("printdatas");

//// method.invoke(pepperobject, null); ;

// methodinfo methods = typepepper.getmethods();//獲取pepper類的所用方法,不管是公用的還是私有的,也不管是靜態的還是其它繼承的,都能夠獲取。

// foreach (methodinfo item in methods)

// // //獲取方法引數基本資訊的使用

// //首先獲取引數所在的方法;

// methodinfo methodinfo = tpepper.getmethod("add");

// parameterinfo parameters = methodinfo.getparameters();

// console.writeline("函式" + methodinfo.name + "的基本資訊");

// foreach (parameterinfo item in parameters)

// // //方法的呼叫

// pepper ptest = new pepper("小辣椒測試", "咖啡色");

// type tptest = ptest.gettype();

// //或者用靜態方法type tptest=type.gettype("reflectionexam.pepper");

// methodinfo tpmethod = tptest.getmethod("add");

//  object parame = new object ;

//// object paramers = new object ;

// //int sum = (int)tpmethod.invoke(tptest, paramers);

// int sum=(int)tpmethod.invoke(ptest, parame);//輸出mronginfo welcome to you

// console.writeline("sum:" + sum);//輸出240

// //objectpas=new object;

// //methodinfo tpmethodpt=typepepper.getmethod("add");

// //int sum=(int)tpmethodpt.invoke(ptest, pas);

// try;

object pepperinstance = activator.createinstance(typepepper, conparameters);

//  typepepper.invokemember("name", bindingflags.getfield|bindingflags.public , null, pepperobject, null);

//  console.writeline("getname:" + name);

console.writeline( typepepper.getfield("name", bindingflags.instance|bindingflags.public).tostring());

typepepper.invokemember("printdatas", bindingflags.invokemethod, null, pepperinstance, null);//注意如果printdatas為私有的,將因為找不到該方法,而引發異常.

console.writeline(typepepper.getfield("name", bindingflags.public|bindingflags.instance).tostring());

system.reflection.propertyinfo pros = typepepper.getproperties();//獲取pepper類中的所有的屬性

foreach (var item in pros)

showmessage show = (showmessage)delegate.createdelegate(typeof(reflectionexam.program.showmessage), pepperinstance, "showmsg");

show("ganquanfu", "黑色");

}catch (system.exception ex)

"finished");

console.read();

}public static activationcontext t }}

反射簡介 C 特性和反射

net編譯器的任務之一就是為所有定義和引用的型別生成元資料描述。除了程式集中標準的元資料外,net平台還支援特定 attribute 把更多的元資料嵌入到程式集中。net特性擴充套件了抽象的system.attribute基類,net中有很多預定義的特性,例如 dllimport obsolete ...

C 特性和反射的應用(一)

之前的專案使用了乙個輕量級的開源orm框架fluentdata,因為專案中領域模型和實體模型合在一起,新增資料時就會出現因為實體和資料庫表對應有差異而不能直接儲存 某些實體的屬性在資料庫表裡沒有對應的字段 通過檢視fluentdata的源 發現可以給實體裡面無法對應資料庫表字段的屬性新增乙個自定義特...

C 使用反射訪問特性

ms註解 如果沒有檢索自定義特性的資訊和對其進行操作的方法,則定義自定義特性並將其放置在源 中就沒有意義。使用反射,可檢索用自定義特性定義的資訊。主要方法是getcustomattributes,它返回物件陣列,這些物件在執行時等效於源 特性。此方法具有多個過載版本 例項 multiuse attr...