您好,登錄后才能下訂單哦!
這篇文章主要講解了“C#中反射方法的用法”,文中的講解內(nèi)容簡單清晰,易于學(xué)習(xí)與理解,下面請(qǐng)大家跟著小編的思路慢慢深入,一起來研究和學(xué)習(xí)“C#中反射方法的用法”吧!
首先我們建立一個(gè)類庫,將它生成為HelloWorld.dll:
usingSystem; namespaceWebtest ...{ publicinterfaceinterface1 ...{ intadd(); } publicclassReflectTest:interface1 ...{ publicStringWrite; privateStringWritec; publicStringWritea ...{ get ...{ returnWrite; } set ...{ Write=value; } } privateStringWriteb ...{ get ...{ returnWritec; } set ...{ Writec=value; } } publicReflectTest() ...{ this.Write="Write"; this.Writec="Writec"; } publicReflectTest(stringstr1,stringstr2) ...{ this.Write=str1; this.Writec=str2; } publicstringWriteString(strings,intb) ...{ return"歡迎您,"+s+"---"+b;; } publicstaticstringWriteName(strings) ...{ return"歡迎您光臨,"+s; } publicstringWriteNoPara() ...{ return"您使用的是無參數(shù)方法"; } privatestringWritePrivate() ...{ return"私有類型的方法"; } publicintadd() ...{ return5; } } }
然后,建立再建立一個(gè)項(xiàng)目引入該HelloWorld.dll:
usingSystem;
usingSystem.Threading;
usingSystem.Reflection;
classTest
...{
delegatestringTestDelegate(stringvalue,intvalue1);
staticvoidMain()
...{
//AssemblyAssemblyt=Assembly.LoadFrom("HelloWorld.dll");與下面相同的效果
AssemblyAssemblyt=Assembly.Load("HelloWorld");
foreach(Typeaaaint.GetTypes())
...{
//Console.Write(aaa.Name);//顯示該dll下所有的類
}
Module[]modules=t.GetModules();
foreach(Modulemoduleinmodules)
...{
//Console.WriteLine("modulename:"+module.Name);//顯示模塊的名字本例為"HelloWorld.dll"
}
Typea=typeof(Webtest.ReflectTest);//得到具體的類的類型,和下面一個(gè)效果
//Typea=t.GetType("Webtest.ReflectTest");//
//Console.Write(a.Name);
string[]bb=...{"aaaa","bbbbb"};
objectobj=Activator.CreateInstance(a,bb);//創(chuàng)建該類的實(shí)例,后面的bb為有參構(gòu)造函數(shù)的參數(shù)
//objectobj=t.CreateInstance("Webtest.ReflectTest");//與上面方法相同
MethodInfo[]miArr=a.GetMethods();
foreach(MethodInfomi0inmiArr)
...{
//Console.Write(mi0.Name);//顯示所有的共有方法
}
MethodInfomi=a.GetMethod("WriteString");//顯示具體的方法
object[]aa=...{"使用的是帶有參數(shù)的非靜態(tài)方法",2};
strings=(string)mi.Invoke(obj,aa);//帶參數(shù)方法的調(diào)用
MethodInfomi1=a.GetMethod("WriteName");
String[]aa1=...{"使用的是靜態(tài)方法"};
strings1=(string)mi1.Invoke(null,aa1);//靜態(tài)方法的調(diào)用
MethodInfomi2=a.GetMethod("WriteNoPara");
strings2=(string)mi2.Invoke(obj,null);//不帶參數(shù)的方法調(diào)用
MethodInfomi3=a.GetMethod("WritePrivate",BindingFlags.Instance|BindingFlags.NonPublic);
strings3=(string)mi3.Invoke(obj,null);//私有類型方法調(diào)用
//Console.Write(s3);
PropertyInfo[]piArr=a.GetProperties
(BindingFlags.Instance|BindingFlags.NonPublic|BindingFlags.Public);foreach(PropertyInfopiinpiArr)
...{
//Console.Write(pi.Name);//顯示所有的方法
}
PropertyInfopi1=a.GetProperty("Writea");
//pi1.SetValue(obj,"Writea",null);
//Console.Write(pi1.GetValue(obj,null));
PropertyInfopi2=a.GetProperty
("Writeb",BindingFlags.Instance|BindingFlags.NonPublic|BindingFlags.Public);pi2.SetValue(obj,"Writeb",null);
//Console.Write(pi2.GetValue(obj,null));
FieldInfofi1=a.GetField("Write");
//Console.Write(fi1.GetValue(obj));
ConstructorInfo[]ci1=a.GetConstructors();
foreach(ConstructorInfociinci1)
...{
//Console.Write(ci.ToString());//獲得構(gòu)造函數(shù)的形式
}
ConstructorInfoasCI=a.GetConstructor(newType[]...{
typeof(string),typeof(string)});
//Console.Write(asCI.ToString());
Webtest.interface1obj1=(Webtest.interface1)t.CreateInstance
("Webtest.ReflectTest");Webtest.ReflectTestobj2=(Webtest.ReflectTest)t.CreateInstance("Webtest.ReflectTest");
//Console.Write(obj1.add());典型的工廠模式
foreach(Typettint.GetTypes())
...{
if(tt.GetInterface("interface1")!=null)
...{
Webtest.interface1obj3=(Webtest.interface1)Activator.CreateInstance(a);
//Console.Write(obj3.add());
}
}
TestDelegatemethod=(TestDelegate)Delegate.CreateDelegate
(typeof(TestDelegate),obj,"WriteString");//動(dòng)態(tài)創(chuàng)建委托的簡單例子
Console.Write(method("str1",2));
Console.Read();
}
}
感謝各位的閱讀,以上就是“C#中反射方法的用法”的內(nèi)容了,經(jīng)過本文的學(xué)習(xí)后,相信大家對(duì)C#中反射方法的用法這一問題有了更深刻的體會(huì),具體使用情況還需要大家實(shí)踐驗(yàn)證。這里是億速云,小編將為大家推送更多相關(guān)知識(shí)點(diǎn)的文章,歡迎關(guān)注!
免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點(diǎn)不代表本網(wǎng)站立場,如果涉及侵權(quán)請(qǐng)聯(lián)系站長郵箱:is@yisu.com進(jìn)行舉報(bào),并提供相關(guān)證據(jù),一經(jīng)查實(shí),將立刻刪除涉嫌侵權(quán)內(nèi)容。