舉例:type tdate = typeof
(system.datetime);
object result = tdate.invokemember("
now"
,
bindingflags.getproperty,
null, null, new object[0
]);
console.writeline(result.tostring());
例2:
/*注意:下面備註的方法都是其他類的方法,比如:testclass類方法
*/testclass tc = new testclass (); //
addup為tc的方法
tc.gettype().invokemember ("
addup
", bindingflags.public |bindingflags.instance |bindingflags.createinstance,
null, tc, new
object
{});
/*public void addup ()
times", methodcalled);
} *///
----------------------------下面傳引數 執行computesum方法 帶有兩個引數
type t = typeof
(testclass);
object args = new
object ;
object result = t.invokemember ("
computesum
", bindingflags.invokemethod, null, null
, args);
/*public static double computesum (double d1, double d2)
*///
-----------sayhello為靜態方法呼叫
t.invokemember ("
sayhello
", bindingflags.invokemethod | bindingflags.public | bindingflags.static, null, null, new
object
{});
//-----------例項方法呼叫
testclass c = new
testclass ();
c.gettype().invokemember (
"addup
", bindingflags.invokemethod, null, c, new
object
{});
c.gettype().invokemember (
"addup
", bindingflags.public |bindingflags.instance |bindingflags.createinstance,
null, c, new
object
{});
//----------獲取字段
result = t.invokemember ("
name
", bindingflags.getfield | bindingflags.getproperty, null, c, new
object
{});
result = t.invokemember ("
value
", bindingflags.getfield | bindingflags.getproperty, null, c, new
object
{});
/*public string name;
public object value
} */result = t.invokemember ("
name
", bindingflags.getfield, null, c, new
object
{});
//---------設定字段
t.invokemember ("
name
", bindingflags.setfield, null, c, new
object ); //
newname設定的屬性值
//---------呼叫類方法 帶引數
object argvalues = new
object ;
string argnames = new string ;
t.invokemember (
"printname
", bindingflags.invokemethod, null, null, argvalues, null, null
, argnames);
/*public static void printname (string firstname, string lastname)
,", lastname,firstname);
} */testclass obj = new
testclass();
system.reflection.methodinfo methinfo =obj.gettype().getmethod(
"printname
");
methinfo.invoke(obj,bindingflags.suppresschangetype |bindingflags.invokemethod,
null,new
object
,null
);
methinfo = obj.gettype().getmethod("
printname
");
methinfo.invoke(obj,bindingflags.ignorecase | //
忽略大小寫 指定當繫結時不應考慮成員名的大小寫
bindingflags.invokemethod, null,new
object
,null
);
methinfo = obj.gettype().getmethod("
printname
");
methinfo.invoke(obj,bindingflags.ignorereturn | //
在 com interop 中用於指定可以忽略成員的返回值
bindingflags.invokemethod, null,new
object
,null
);
methinfo = obj.gettype().getmethod("
printname
");
methinfo.invoke(obj,bindingflags.optionalparambinding |bindingflags.invokemethod,
null,new
object
,null
);
methinfo = obj.gettype().getmethod("
printname
");
methinfo.invoke(obj,bindingflags.exactbinding |bindingflags.invokemethod,
null,new
object
,null
);
methinfo = obj.gettype().getmethod("
printname
");
methinfo.invoke(obj,bindingflags.flattenhierarchy |bindingflags.invokemethod,
null,new
object
,null
);
//----------呼叫乙個預設的方法
type t3 = typeof
(testclass2);
/*[defaultmemberattribute ("printtime")]
public class testclass2
} */t3.invokemember (
"", bindingflags.invokemethod | bindingflags.default, null, new testclass2(), new
object
{});
//---------呼叫乙個引用方法
methodinfo m = t.getmethod("
swap
");
args = new
object[2
];
args[
0] = 1
; args[
1] = 2
; m.invoke(
newtestclass(),args); /*
public void swap(ref int a, ref int b) 交換 a b
*/
sscanf用法簡析
1.常見用法。char buf 512 sscanf 123456 s buf printf s n buf 結果為 123456 2.取指定長度的字串。如在下例中,取最大長度為4位元組的字串。sscanf 123456 4s buf printf s n buf 結果為 1234 3.取到指定字元...
DataReader的用法程式簡析
2015 07 05 using system using system.collections.generic using system.linq using system.text using system.threading.tasks using system.data using syst...
Python random模組用法解析及簡單示例
用法示例 import random 1 隨機小數 print random.random 獲取大於0且小於1 之間的小數 random.random print random.uniform 1,4 獲取大於1小於3的小數 2 隨機整數 print 9 獲取大於等於1且小於等於9之間的整數 pri...