首先建立一個類庫,將它產生ClassLibrary1.dll
using System;<br />using System.Collections.Generic;<br />using System.Text;</p><p>namespace WebTest<br />{<br /> public class ReflectTest<br /> {<br /> private string word;</p><p> public String Word<br /> {<br /> get<br /> {<br /> return word;<br /> }<br /> set<br /> {<br /> word = value;<br /> }<br /> }</p><p> public ReflectTest(string w)<br /> {<br /> this.word = w;<br /> }</p><p> public ReflectTest()<br /> {<br /> this.word = "word";<br /> }</p><p> public string WriteString(String s)<br /> {<br /> return "Welcome + " + s;<br /> }</p><p> public static string WriteStatic(String name)<br /> {<br /> return "Static : Welcome + " + name;<br /> }</p><p> public string WriteNoPara()<br /> {<br /> return "NoPara : Welcome !";<br /> }<br /> private String WritePrivate()<br /> {<br /> return "Private ";<br /> }<br /> }<br /> public class TestClass<br /> {<br /> }<br />}<br />
然後建立立一個項目以入該dll(代碼裡有e文注釋)
using System;<br />using System.Collections.Generic;<br />using System.Text;<br />using System.Reflection;</p><p>namespace TestReflect<br />{<br /> class Program<br /> {<br /> static void Main(string[] args)<br /> {<br /> System.Reflection.Assembly ass;<br /> Type type;<br /> Object obj;<br /> try<br /> {<br /> ass = Assembly.Load("ClassLibrary1");</p><p> Console.WriteLine("-----------All the class In this dll-----------");<br /> foreach (Type t in ass.GetTypes())<br /> {<br /> Console.WriteLine(t.Name);<br /> }</p><p> Console.WriteLine("---------All the Module In this dll----------");<br /> Module[] miar = ass.GetModules();<br /> foreach (Module m in miar)<br /> {<br /> Console.WriteLine(m.Name);<br /> }</p><p> type = ass.GetType("WebTest.ReflectTest");</p><p> Console.WriteLine("-----------All the method In WebTest.ReflectTest-----------");<br /> MethodInfo[] miarr = type.GetMethods();<br /> foreach (MethodInfo m in miarr)<br /> {<br /> Console.WriteLine(m.Name);<br /> }</p><p> Console.WriteLine("-----------All the Property In WebTest.ReflectTest-----------");<br /> PropertyInfo[] piarr = type.GetProperties(BindingFlags.Instance | BindingFlags.NonPublic | BindingFlags.Public);<br /> foreach (PropertyInfo p in piarr)<br /> {<br /> Console.WriteLine(p.Name);<br /> }</p><p> Console.WriteLine("----------Call some methods in WebTest.ReflectTest-----------");<br /> obj = ass.CreateInstance("WebTest.ReflectTest");</p><p> MethodInfo method = type.GetMethod("WriteString");<br /> String s = (String)method.Invoke(obj, new string[] { "WriteString" });<br /> Console.WriteLine(s);</p><p> method = type.GetMethod("WriteStatic");<br /> s = (String)method.Invoke(null, new string[] { "WriteName" });<br /> Console.WriteLine(s);</p><p> method = type.GetMethod("WriteNoPara");<br /> s = (String)method.Invoke(obj, null);<br /> Console.WriteLine(s);</p><p> method = type.GetMethod("WritePrivate", BindingFlags.Instance | BindingFlags.NonPublic);<br /> s = (String)method.Invoke(obj, null);<br /> Console.WriteLine(s);</p><p> Console.WriteLine("---------Use the Property---------");<br /> PropertyInfo pi = type.GetProperty("Word");<br /> pi.SetValue(obj, "Word", null);<br /> Console.WriteLine(pi.GetValue(obj,null));</p><p> Console.WriteLine("--------- Get All Constructor ----------");<br /> ConstructorInfo[] ci = type.GetConstructors();<br /> foreach (ConstructorInfo c in ci)<br /> {<br /> Console.WriteLine(c.ToString());<br /> }</p><p> Console.WriteLine("-------- Construct --------");<br /> ConstructorInfo ci1 = type.GetConstructor(new Type[] { typeof(string) });<br /> WebTest.ReflectTest wr1 = (WebTest.ReflectTest)ci1.Invoke(new string[] { "XXX" });<br /> Console.WriteLine(wr1.Word);</p><p> ConstructorInfo ci2 = type.GetConstructor(new Type[0] );<br /> WebTest.ReflectTest wr2 = (WebTest.ReflectTest)ci2.Invoke(null);<br /> Console.WriteLine(wr2.Word);</p><p> }<br /> catch (Exception ex)<br /> {<br /> Console.WriteLine(ex);<br /> }<br /> finally<br /> {<br /> ass = null;<br /> type = null;<br /> obj = null;<br /> }</p><p> }<br /> }<br />}<br />
謝謝.