此文大部分的东西引自其它的一个网,具体不太清楚了! 在这里说声不好意思
进入正文:
反射在我个人看来就是一个解析DLL并运用的过程
创建一个DLL 类库(类库为HelloWord ,所以产生的DLL 也为HelloWord) 下面是代码,请注意命名空间和类名的不同:
using System; namespace Webtest { public interface interface1 { int add(); } public class ReflectTest:interface1 { public String Write; private String Writec; public String Writea { get { return Write; } set { Write = value; } } private String Writeb { get { return Writec; } set { Writec = value; } } public ReflectTest() { this.Write = "Write"; this.Writec = "Writec"; } public ReflectTest(string str1,string str2) { this.Write = str1; this.Writec = str2; } public string WriteString(string s,int b) { return "欢迎您," + s + "---" + b; ; } public static string WriteName(string s) { return "欢迎您光临," + s; } public string WriteNoPara() { return "您使用的是无参数方法"; } private string WritePrivate() { return "私有类型的方法"; } public int add() { return 5; } } }
真正的反射开始了:
using System; //using System.Threading; using System.Reflection;// 反射所需要的命名空间 using System.Collections.Generic; using System.ComponentModel; class Test { delegate string TestDelegate(string value,int value1); static void Main() { AssemblyName an = new AssemblyName("HelloWord"); List<object> test = new List<object>(); //----------------------------------------------------------------------------------------------------------片断2 //Assembly t = Assembly.LoadFrom("HelloWord.dll"); 与下面相同的效果 Assembly t = Assembly.Load("HelloWord"); //********************************************************************** Console.WriteLine("-------------------------------------------All Class in DLL: "); foreach (Type aaa in t.GetTypes()) { Console.WriteLine(aaa.Name + ":" +aaa.FullName); //显示该dll下所有的类 if (aaa.IsClass) test.Add(Activator.CreateInstance(aaa)); else if (aaa.IsInterface) Console.WriteLine("{0} is InterFace", aaa.FullName); } Console.WriteLine("-------------------------------------------"); MethodInfo[] testMeth= test[0].GetType().GetMethods(); Type b = typeof(Webtest.ReflectTest);//得到具体的类的类型,和下面一个效果 (test[0] as Webtest.ReflectTest).WriteString("a", 1);//这里是引用进来才可以那样做,如果那个HelloWord 没有引用进来的话, 那么这里是不能那样写的! testMeth[0].Invoke(test[0],null); //********************************************************************** Console.WriteLine("-------------------------------------------All Modules in DLL:"); Module[] modules = t.GetModules(); foreach (Module module in modules) { Console.WriteLine("module name:" + module.Name);//显示模块的名字本例为"HelloWorld.dll" } Console.WriteLine("-------------------------------------------"); //********************************************************************** //如果没有把HelloWord引用到工程的话, 这个类型将不能用,要用后面那一种 Type a = typeof(Webtest.ReflectTest);//得到具体的类的类型,和下面一个效果 //Type a = t.GetType("Webtest.ReflectTest");// Console.WriteLine(a.Name + "is Class :" + a.IsClass); //********************************************************************** string[] bb = { "aaaa", "bbbbb" }; object obj = Activator.CreateInstance(a,bb); //创建该类的实例,后面的bb为有参构造函数的参数 //object obj = t.CreateInstance("Webtest.ReflectTest");//与上面方法相同 //********************************************************************** Console.WriteLine("-------------------------------------------All Public Method in DLL:"); MethodInfo[] miArr = a.GetMethods(); foreach (MethodInfo mi0 in miArr) { Console.WriteLine(mi0.Name); //显示所有的共有方法 } Console.WriteLine("-------------------------------------------"); Console.WriteLine("-------------------------------------------All Information: "); MemberInfo[] miPar = a.GetMembers(); //MemberInfo[] miPar2 = a.FindMembers() foreach (MemberInfo mbi in miPar) { Console.WriteLine(mbi); //显示所有的共有方法 } Console.WriteLine("-------------------------------------------"); //********************************************************************** MethodInfo mi = a.GetMethod("WriteString");//显示具体的方法 object[] aa= {"使用的是带有参数的非静态方法",2}; string s = (string)mi.Invoke(obj,aa); //带参数方法的调用 MethodInfo mi1 = a.GetMethod("WriteName"); String[] aa1 = {"使用的是静态方法"}; string s1 = (string)mi1.Invoke(null, aa1); //静态方法的调用 MethodInfo mi2 = a.GetMethod("WriteNoPara"); string s2 = (string)mi2.Invoke(obj, null); //不带参数的方法调用 MethodInfo mi3 = a.GetMethod("WritePrivate",BindingFlags.Instance | BindingFlags.NonPublic); string s3 = (string)mi3.Invoke(obj, null); //私有类型方法调用 Console.WriteLine("Private ..---->" + s3); //********************************************************************** PropertyInfo[] piArr = a.GetProperties(BindingFlags.Instance | BindingFlags.NonPublic | BindingFlags.Public); foreach (PropertyInfo pi in piArr) { Console.WriteLine(pi.Name); //显示所有的属性 } //********************************************************************** PropertyInfo pi1=a.GetProperty("Writea"); //pi1.SetValue(obj, "Writea", null); Console.WriteLine(pi1.GetValue(obj, null)); PropertyInfo pi2 = a.GetProperty("Writeb", BindingFlags.Instance | BindingFlags.NonPublic | BindingFlags.Public); pi2.SetValue(obj, "Writeb", null); Console.WriteLine(pi2.GetValue(obj, null)); FieldInfo fi1 = a.GetField("Write"); Console.WriteLine(fi1.GetValue(obj)); //********************************************************************** ConstructorInfo[] ci1 = a.GetConstructors(); foreach (ConstructorInfo ci in ci1) { Console.WriteLine(ci.ToString()); //获得构造函数的形式 } ConstructorInfo asCI = a.GetConstructor(new Type[] { typeof(string), typeof(string) }); Console.WriteLine(asCI.ToString()); //********************************************************************** Webtest.interface1 obj1 = (Webtest.interface1)t.CreateInstance("Webtest.ReflectTest"); Webtest.ReflectTest obj2 = (Webtest.ReflectTest)t.CreateInstance("Webtest.ReflectTest"); //Console.Write(obj1.add());典型的工厂模式 //********************************************************************** foreach (Type tt in t.GetTypes()) { if (tt.GetInterface("interface1")!=null) { Webtest.interface1 obj3 = (Webtest.interface1)Activator.CreateInstance(a); Console.WriteLine(obj3.add()); } } //********************************************************************** TestDelegate method = (TestDelegate)Delegate.CreateDelegate(typeof(TestDelegate), obj, "WriteString"); //动态创建委托的简单例子 Console.WriteLine(method("str1", 2)); //********************************************************************** ConstructorInfo asCI1 = a.GetConstructor(new Type[0]); Webtest.ReflectTest obj5 = (Webtest.ReflectTest)asCI1.Invoke(null); //通过无参构造函数实例化的方法 //Console.Write(obj5.Writea); ConstructorInfo asCI2 = a.GetConstructor(new Type[] { typeof(string), typeof(string) }); //通过有参构造函数实例化的方法 Webtest.ReflectTest obj6 = (Webtest.ReflectTest)asCI2.Invoke(bb); Console.WriteLine(obj6.Writea); //********************************************************************** Console.ReadLine(); } }