C 反射方法學習

2021-09-06 23:35:28 字數 3487 閱讀 2633

1 usingsystem;

2 namespacewebtest

3 …

8 publicclassreflecttest:inte***ce1

9 …

18 set

19 …

22 }

23 privatestringwriteb

24 …

29 set

30 …

33 }

34 publicreflecttest()

35 …

39 publicreflecttest(stringstr1,stringstr2)

40 …

44 publicstringwritestring(strings,intb)

45 …

48 publicstaticstringwritename(strings)

49 …

52 publicstringwritenopara()

53 …

56 privatestringwriteprivate()

57 …

60 publicintadd()

61 …

64 }

65 }

66 然後,建立再建立乙個專案引入該helloworld.dll:

67 usingsystem;

68 usingsystem.threading;

69 usingsystem.reflection;

70 classtest

71 …

81 modulemodules=t.getmodules();

82 foreach(modulemoduleinmodules)

83 …

86 typea=typeof(webtest.reflecttest);//得到具體的類的型別,和下面乙個效果

87 //typea=t.gettype("webtest.reflecttest");//

88 89 stringbb=…;

90 objectobj=activator.createinstance(a,bb);//建立該類的例項,後面的bb為有參建構函式的引數

91 //objectobj=t.createinstance("webtest.reflecttest");//與上面方法相同

92 methodinfomiarr=a.getmethods();

93 foreach(methodinfomi0inmiarr)

94 …

97 methodinfomi=a.getmethod("writestring");//顯示具體的方法

98 objectaa=…;

99 strings=(string)mi.invoke(obj,aa);//帶引數方法的呼叫

100 methodinfomi1=a.getmethod("writename");

101 stringaa1=…;

102 strings1=(string)mi1.invoke(null,aa1);//靜態方法的呼叫

103 methodinfomi2=a.getmethod("writenopara");

104 strings2=(string)mi2.invoke(obj,null);//不帶引數的方法呼叫

105 methodinfomi3=a.getmethod("writeprivate",bindingflags.instance|bindingflags.nonpublic);

106 strings3=(string)mi3.invoke(obj,null);//私有型別方法呼叫

107

108 propertyinfopiarr=a.getproperties

109 (bindingflags.instance|bindingflags.nonpublic|bindingflags.public);

110 foreach(propertyinfopiinpiarr)

111 …

114 propertyinfopi1=a.getproperty("writea");

115 "writea",null);

116

117 propertyinfopi2=a.getproperty

118 ("writeb",bindingflags.instance|bindingflags.nonpublic|bindingflags.public);

119 pi2.setvalue(obj,"writeb",null);

120

121 fieldinfofi1=a.getfield("write");

122

123 constructorinfoci1=a.getconstructors();

124 foreach(constructorinfociinci1)

125 …

128 constructorinfoasci=a.getconstructor(newtype…);

130

131 webtest.inte***ce1obj1=(webtest.inte***ce1)t.createinstance

132 ("webtest.reflecttest");

133 webtest.reflecttestobj2=(webtest.reflecttest)t.createinstance("webtest.reflecttest");

134 典型的工廠模式

135 foreach(typettint.gettypes())

136 …

142 }

143 testdelegatemethod=(testdelegate)delegate.createdelegate

144 (typeof(testdelegate),obj,"writestring");

145 //動態建立委託的簡單例子

146 console.write(method("str1",2));

147 console.read();

148 }

149 }

在這裡我把我們常用的方法,屬性,等全部整理了出來,大家不要嫌棄亂,靜下心來,自己按照我的分隔一部分一部分的來,保證你對c#反射方法的學習,會事半功倍。當然有關於其方法我會繼續補充,想了這麼些就先寫下來吧。

C 擴充套件方法 學習

c 中可以定義擴充套件方法,還可以為集合做擴充套件方法。示例如下 using system using system.collections.generic using myspace 注意 引入擴充套件方法的空間 namespace con 1 先生。with xugang console.wri...

swift方法學習

1.例項方法 2.型別方法 型別方法和oc中的類方法相似 結構體和列舉能夠定義方法是swift與oc c的主要區別之一。在oc中,類是唯一能定義方法的型別。但在swift中,你不僅能選擇是否要定義乙個類 結構體 列舉,還能靈活的在你建立的型別 類 結構體 列舉 上定義方法。例項方法 例項方法的語法和...

NIO ByteBuffer方法學習

建立heapbytebuffer例項,大小為1024位元組 bytebuffer heapbytebuffer bytebuffer.allocate 1024 建立directbytebuffer例項,大小為1024位元組 這裡的bytebuffer是directbytebuffer,不是heap...