using System; using System.Collections.Generic; using System.ComponentModel; using System.Data; using System.Drawing; using System.Text; using System.Windows.Forms; using System.Reflection; namespace fanshetest { public delegate void bb(string str); public partial class Form1 : Form { System.Reflection.Assembly a; System.Reflection.Assembly b; public Form1() { InitializeComponent(); a = System.Reflection.Assembly.LoadFrom("Class1.DLL"); } private void button1_Click(object sender, EventArgs e) { One(); } //没有传参数,返回一个类型; private void One() { //再另建一个项目,在项目中引用上面生成的ClassLibrary1.DLL System.Type t = a.GetType("Class1.Class1"); //动态生成ClassLibrary1.Class类的实例 Object theObj = System.Activator.CreateInstance(t); //参数信息,GetSum需要两个string参数 //System.Type[] paramTypes = new System.Type[2]; //paramTypes[0] = System.Type.GetType("System.String"); //paramTypes[1] = System.Type.GetType("System.String"); //System.Reflection.MethodInfo mi = t.GetMethod("aa", paramTypes); System.Reflection.MethodInfo mi = t.GetMethod("bb"); //参数值 //Object[] parameters = new Object[2]; //parameters[0] = 3; //parameters[1] = 4; Object returnValue = mi.Invoke(theObj, null); this.textBox1.Text = returnValue.ToString(); } //没有返回值,调用的是直接执行函数 private void None_void() { //再另建一个项目,在项目中引用上面生成的ClassLibrary1.DLL System.Type t = a.GetType("Class1.Class2"); //动态生成ClassLibrary1.Class类的实例 Object theObj = System.Activator.CreateInstance(t); //参数信息,GetSum需要两个string参数 System.Type[] paramTypes = new System.Type[2]; //此处调用方法,如果有参数只需要在括号的后面加上"," 加上参数的type[]类型的参数 System.Reflection.MethodInfo mi = t.GetMethod("Mes"); Object returnValue = mi.Invoke(theObj, null); } //没有返回值,传出参数接收返回值; private void Two() { Type t = a.GetType("Class1.Class1"); Object theObj = Activator.CreateInstance(t); Type[] types=new Type[2]; types[0]=Type.GetType("System.Int32"); types[1]=Type.GetType("System.Int32"); Object[] obj=new Object[2]; obj[0]=2; obj[1]=3; MethodInfo mi = t.GetMethod("aa",types); Object returnValue=mi.Invoke(theObj,obj); this.textBox1.Text = returnValue.ToString(); } //获取同一个命名空间的反射出值 private void Local() { Type t=Type.GetType("fanshetest1.Class1"); //创建一个 实例 Object theObj = Activator.CreateInstance(t); Type[] types=new Type[2]; types[0]=Type.GetType("System.Int32"); types[1] = Type.GetType("System.Int32"); Object[] obj = new Object[2]; obj[0] = 2; obj[1] = 3; MethodInfo mi = t.GetMethod("aa",types); Object returnValue = mi.Invoke(theObj,obj); this.textBox1.Text = returnValue.ToString(); } //获取出一个属性 private void attribute() { a = Assembly.LoadFrom("Class1.dll"); Type t = a.GetType("Class1.Class1"); t = Type.GetType("fanshetest1.Class1"); Object theObj = Activator.CreateInstance(t); t.GetProperty("AB").SetValue(theObj,"a",null); this.textBox1.Text = t.GetProperty("AB").GetValue(theObj, null).ToString(); } //获取出静态的函数; private void static_() { a = System.Reflection.Assembly.LoadFrom("Class1.dll"); Type t = a.GetType("Class1.Class1"); t = Type.GetType("fanshetest1.Class1"); //创建一个实例 Object theObj = Activator.CreateInstance(t); //创建一个方法的实例 MethodInfo mi = t.GetMethod("cc"); Object returnValue = mi.Invoke(theObj, null); this.textBox1.Text = returnValue.ToString(); } //获取出变量; private void variable() { a = Assembly.LoadFrom("Class1.dll"); Type t = a.GetType("Class1.Class1"); t = Type.GetType("fanshetest1.Class1"); Object theObj = Activator.CreateInstance(t); MethodInfo mi = t.GetMethod("a"); t.GetField("a").SetValue(theObj,"a"); this.textBox1.Text = t.GetField("a").GetValue(theObj).ToString(); } //获取出私有变量反射值; private void private_() { } //构造函数 private void gouzaohanshu() { Type t = a.GetType("Class1.Class1"); t = Type.GetType("fanshetest1.Class1"); //创建构造函数类型 Type[] structure_Type = new Type[1]; structure_Type[0] = Type.GetType("System.String"); //定义构造函数传参类型 Object[] structure_Obj = new Object[1]; structure_Obj[0] = "aa"; //创建一个 实例 Object theObj = Activator.CreateInstance(t,structure_Obj); //MethodInfo structure_Mi = t.GetConstructor(structure_Type); //MethodInfo structure_Mi = t.GetMethod("Class1", structure_Type); //structure_Mi.Invoke(theObj, structure_Obj); MethodInfo mi = t.GetMethod("a"); //t.GetField("a").SetValue(theObj, "a"); this.textBox1.Text = t.GetField("a").GetValue(theObj).ToString(); } //调用事件 private void delegate_() { a = Assembly.LoadFrom("Class1.dll"); Type t = a.GetType("Class1.Class1"); Object theObj = Activator.CreateInstance(t); //MethodInfo mi = t.GetMethod("event_Test"); //mi.Invoke(theObj, null); //System.Reflection.Emit命名空间的类提供了一种特殊形式的反射,使您能够在运行时构造类型。反射也可用于创建称作类型浏览器的应用程序,它使用户能够选择类型,然后查看有关选定类型的信息。 //使用 EventInfo 来了解如下的类似信息:事件的名称、事件处理程序数据类型、自定义属性、声明类型和反射类型等;并添加或移除事件处理程序。 EventInfo ei = t.GetEvent("delet_Event",BindingFlags.CreateInstance|BindingFlags.Instance|BindingFlags.Public); Type t1 = a.GetType("Class1.bb"); Delegate d = Delegate.CreateDelegate(t1, this, "TextShow"); ei.AddEventHandler(theObj, d); Type[] typ=new Type[1]; typ[0]=Type.GetType("System.String"); Object[] obj = new Object[1]; obj[0] = "aaaa"; //使用MethodInfo了解以下信息:方法的名称、返回类型、参数、访问修饰符(如 public 或 private)和实现详细信息(如 abstract 或 virtual)等。使用 Type 的 GetMethods 或 GetMethod 方法来调用特定的方法。 MethodInfo mi = t.GetMethod("event_Test",typ); mi.Invoke(theObj, obj); } private void TextShow(string str) { this.textBox1.Text = str; } } }