如果想复用别人写的.NET程序但手上只有一个编译过的EXE,那么方法之一是用Reflection(反射)。下面是一些例子,初次上手的人可以参考。
先假设我们要复用的第三方应用程序EXE是由下面的代码编译成的:
view plaincopy to clipboardprint?
using System;
namespace MyNamespace
{
public class MyApp
{
public MyNestedObject myNestedObject=null;
public class MyNestedObject
{
public string name;
}
public enum FourSeasonsEnum
{
spring,
summer,
autumn,
winter
}
public MyApp()
{
}
public MyApp(MyNamespace.MyForm form)
{
}
public MyNestedObject Foo1(FourSeasonsEnum season)
{
return this.myNestedObject;
}
public string Foo2()
{
return "";
}
static void Main()
{
}
}
public class MyForm
{
public MyForm()
{
}
}
}
using System;
namespace MyNamespace
{
public class MyApp
{
public MyNestedObject myNestedObject=null;
public class MyNestedObject
{
public string name;
}
public enum FourSeasonsEnum
{
spring,
summer,
autumn,
winter
}
public MyApp()
{
}
public MyApp(MyNamespace.MyForm form)
{
}
public MyNestedObject Foo1(FourSeasonsEnum season)
{
return this.myNestedObject;
}
public string Foo2()
{
return "";
}
static void Main()
{
}
}
public class MyForm
{
public MyForm()
{
}
}
}
以下是一些直接引用时常见的调用如何用Reflection来改写:
1. 用不含参数的构造函数生成对象
view plaincopy to clipboardprint?
直接引用时,代码是
MyNamespace.MyApp app=new MyNamespace.MyApp();
用Reflection来调用的话就需要这么写(记得using System.Reflection)
Assembly assem=Assembly.LoadFile(@"C:/Home/Workspace/MyNamespace/myapp.exe");
Type MyAppType=assem.GetType("MyNamespace.MyApp");
ConstructorInfo MyAppType_Constructor=MyAppType.GetConstructor(new Type[]{});
object app=MyAppType_Constructor.Invoke(new object[]{});
直接引用时,代码是
MyNamespace.MyApp app=new MyNamespace.MyApp();
用Reflection来调用的话就需要这么写(记得using System.Reflection)
Assembly assem=Assembly.LoadFile(@"C:/Home/Workspace/MyNamespace/myapp.exe");
Type MyAppType=assem.GetType("MyNamespace.MyApp");
ConstructorInfo MyAppType_Constructor=MyAppType.GetConstructor(new Type[]{});
object app=MyAppType_Constructor.Invoke(new object[]{});
2. 用含参数的构造函数生成对象
view plaincopy to clipboardprint?
直接引用时,代码是
MyNamespace.MyApp app=new MyNamespace.MyApp(new MyNamespace.MyForm());
用Reflection来调用的话就需要这么写
Assembly assem=Assembly.LoadFile(@"C:/Home/Workspace/MyNamespace/myapp.exe");
Type MyAppType=assem.GetType("MyNamespace.MyApp");
Type MyFormType=assem.GetType("MyNamespace.MyForm");
ConstructorInfo MyAppType_Constructor=MyAppType.GetConstructor(new Type[]{MyFormType});
ConstructorInfo MyFormType_Constructor=MyFormType.GetConstructor(new Type[]{});
object form=MyFormType_Constructor.Invoke(new object[]{});
object app=MyAppType_Constructor.Invoke(new object[]{form});
直接引用时,代码是
MyNamespace.MyApp app=new MyNamespace.MyApp(new MyNamespace.MyForm());
用Reflection来调用的话就需要这么写
Assembly assem=Assembly.LoadFile(@"C:/Home/Workspace/MyNamespace/myapp.exe");
Type MyAppType=assem.GetType("MyNamespace.MyApp");
Type MyFormType=assem.GetType("MyNamespace.MyForm");
ConstructorInfo MyAppType_Constructor=MyAppType.GetConstructor(new Type[]{MyFormType});
ConstructorInfo MyFormType_Constructor=MyFormType.GetConstructor(new Type[]{});
object form=MyFormType_Constructor.Invoke(new object[]{});
object app=MyAppType_Constructor.Invoke(new object[]{form});
3. 调用对象的方法
view plaincopy to clipboardprint?
直接引用时,代码是
MyNamespace.MyApp app=new MyNamespace.MyApp();
string str=app.Foo2();
用Reflection来调用的话就需要这么写
Assembly assem=Assembly.LoadFile(@"C:/Home/Workspace/MyNamespace/myapp.exe");
Type MyAppType=assem.GetType("MyNamespace.MyApp");
ConstructorInfo MyAppType_Constructor=MyAppType.GetConstructor(new Type[]{});
object app=MyAppType_Constructor.Invoke(new object[]{});
object str=MyAppType.GetMethod("Foo2").Invoke(app,new object[]{});
直接引用时,代码是
MyNamespace.MyApp app=new MyNamespace.MyApp();
string str=app.Foo2();
用Reflection来调用的话就需要这么写
Assembly assem=Assembly.LoadFile(@"C:/Home/Workspace/MyNamespace/myapp.exe");
Type MyAppType=assem.GetType("MyNamespace.MyApp");
ConstructorInfo MyAppType_Constructor=MyAppType.GetConstructor(new Type[]{});
object app=MyAppType_Constructor.Invoke(new object[]{});
object str=MyAppType.GetMethod("Foo2").Invoke(app,new object[]{});
4. Set/Get成员变量
view plaincopy to clipboardprint?
直接引用时,代码是
MyNamespace.MyApp app=new MyNamespace.MyApp();
MyNamespace.MyApp.MyNestedObject obj=app.myNestedObject;
MyNamespace.MyApp.MyNestedObject obj2=new MyNamespace.MyApp.MyNestedObject();
app.myNestedObject =obj2;
用Reflection来调用的话就需要这么写(注意,这里的MyNestedObject类是nested type,名字要用"MyNamespace.MyApp+MyNestedObject"而不是"MyNamespace.MyApp.MyNestedObject")
Assembly assem=Assembly.LoadFile(@"C:/Home/Workspace/MyNamespace/myapp.exe");
Type MyAppType=assem.GetType("MyNamespace.MyApp");
ConstructorInfo MyAppType_Constructor=MyAppType.GetConstructor(new Type[]{});
object app=MyAppType_Constructor.Invoke(new object[]{});
Type MyNestedObjectType=assem.GetType("MyNamespace.MyApp+MyNestedObject");
FieldInfo MyNestedObjField=MyAppType.GetField("myNestedObject");
object obj=MyNestedObjField.GetValue(app);
ConstructorInfo MyNestedObjectType_Constructor=MyNestedObjectType.GetConstructor(new Type[]{});
object obj2=MyNestedObjectType_Constructor.Invoke(new object[]{});
MyNestedObjField.SetValue(app,obj2);
直接引用时,代码是
MyNamespace.MyApp app=new MyNamespace.MyApp();
MyNamespace.MyApp.MyNestedObject obj=app.myNestedObject;
MyNamespace.MyApp.MyNestedObject obj2=new MyNamespace.MyApp.MyNestedObject();
app.myNestedObject =obj2;
用Reflection来调用的话就需要这么写(注意,这里的MyNestedObject类是nested type,名字要用"MyNamespace.MyApp+MyNestedObject"而不是"MyNamespace.MyApp.MyNestedObject")
Assembly assem=Assembly.LoadFile(@"C:/Home/Workspace/MyNamespace/myapp.exe");
Type MyAppType=assem.GetType("MyNamespace.MyApp");
ConstructorInfo MyAppType_Constructor=MyAppType.GetConstructor(new Type[]{});
object app=MyAppType_Constructor.Invoke(new object[]{});
Type MyNestedObjectType=assem.GetType("MyNamespace.MyApp+MyNestedObject");
FieldInfo MyNestedObjField=MyAppType.GetField("myNestedObject");
object obj=MyNestedObjField.GetValue(app);
ConstructorInfo MyNestedObjectType_Constructor=MyNestedObjectType.GetConstructor(new Type[]{});
object obj2=MyNestedObjectType_Constructor.Invoke(new object[]{});
MyNestedObjField.SetValue(app,obj2);
5. 使用枚举类型
view plaincopy to clipboardprint?
直接引用时,代码是
MyNamespace.MyApp app=new MyNamespace.MyApp();
MyNamespace.MyApp.MyNestedObject obj=app.Foo1(MyNamespace.MyApp.FourSeasonsEnum.spring);
用Reflection来调用的话就需要这么写
Assembly assem=Assembly.LoadFile(@"C:/Home/Workspace/MyNamespace/myapp.exe");
Type MyAppType=assem.GetType("MyNamespace.MyApp");
ConstructorInfo MyAppType_Constructor=MyAppType.GetConstructor(new Type[]{});
object app=MyAppType_Constructor.Invoke(new object[]{});
Type FourSeasonsEnumType=assem.GetType("MyNamespace.MyApp+FourSeasonsEnum");
Array FourSeasonsEnumValues=Enum.GetValues(FourSeasonsEnumType);
object SpringValue=FourSeasonsEnumValues.GetValue(0);
object result=MyAppType.GetMethod("Foo1").Invoke(app,new object[]{SpringValue});
直接引用时,代码是
MyNamespace.MyApp app=new MyNamespace.MyApp();
MyNamespace.MyApp.MyNestedObject obj=app.Foo1(MyNamespace.MyApp.FourSeasonsEnum.spring);
用Reflection来调用的话就需要这么写
Assembly assem=Assembly.LoadFile(@"C:/Home/Workspace/MyNamespace/myapp.exe");
Type MyAppType=assem.GetType("MyNamespace.MyApp");
ConstructorInfo MyAppType_Constructor=MyAppType.GetConstructor(new Type[]{});
object app=MyAppType_Constructor.Invoke(new object[]{});
Type FourSeasonsEnumType=assem.GetType("MyNamespace.MyApp+FourSeasonsEnum");
Array FourSeasonsEnumValues=Enum.GetValues(FourSeasonsEnumType);
object SpringValue=FourSeasonsEnumValues.GetValue(0);
object result=MyAppType.GetMethod("Foo1").Invoke(app,new object[]{SpringValue});
---
最后,只要有可能,应该尽量不用Reflection,因为相比起直接调用,Reflection的性能相当差。
反射实例:
Class1和Form 窗体在同一个命名空间
view plaincopy to clipboardprint?
using System;
using System.Collections.Generic;
using System.Text;
namespace fanshetest1
{
class Class1
{
private string ab="1";
public Class1(string aa)
{
a = aa;
}
public int aa(int x,int y)
{
return x+y+x+y;
}
public string bb()
{
return "bb";
}
public static string cc()
{
return "cc";
}
public string AB
{
get
{
return ab;
}
set
{
ab = value;
}
}
public string a;
}
}
using System;
using System.Collections.Generic;
using System.Text;
namespace fanshetest1
{
class Class1
{
private string ab="1";
public Class1(string aa)
{
a = aa;
}
public int aa(int x,int y)
{
return x+y+x+y;
}
public string bb()
{
return "bb";
}
public static string cc()
{
return "cc";
}
public string AB
{
get
{
return ab;
}
set
{
ab = value;
}
}
public string a;
}
}
Class1和Form 窗体在不同一个命名空间
view plaincopy to clipboardprint?
using System;
using System.Collections.Generic;
using System.Text;
namespace fanshetest1
{
class Class1
{
private string ab="1";
public Class1(string aa)
{
a = aa;
}
public int aa(int x,int y)
{
return x+y+x+y;
}
public string bb()
{
return "bb";
}
public static string cc()
{
return "cc";
}
public string AB
{
get
{
return ab;
}
set
{
ab = value;
}
}
public string a;
}
}
using System;
using System.Collections.Generic;
using System.Text;
namespace fanshetest1
{
class Class1
{
private string ab="1";
public Class1(string aa)
{
a = aa;
}
public int aa(int x,int y)
{
return x+y+x+y;
}
public string bb()
{
return "bb";
}
public static string cc()
{
return "cc";
}
public string AB
{
get
{
return ab;
}
set
{
ab = value;
}
}
public string a;
}
}
下面是如何使用反射操作以上类;
view plaincopy to clipboardprint?
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 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)
{
gouzaohanshu();
}
//没有传参数,返回一个类型;
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();
}
}
}
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 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)
{
gouzaohanshu();
}
//没有传参数,返回一个类型;
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();
}
}
}
反射是审查元数据并收集关于它的类型信息的能力。元数据(编译以后的最基本数据单元)就是一大堆的表,当编译程序集或者模块时,编译器会创建一个类定义表,一个字段定义表,和一个方法定义表等.
反射的主要作用是用来扩展系统和动态调用程序集。
所谓扩展系统就是先把系统写好,系统里面定义接口,后面开发的人去写接口的代码。
此时该系统就要用反射了,系统用反射调用接口,当接口没写,系统利用反射就不会不会出错,此时就等于没实现此功能而已,当接口被人写好了,系统就会自动调用接口的功能展现在系统上。即反射实现即插即用功能。
动态调用程序集就是利用反射去调用编译好的dll,当然此时的dll没有被引用到你所建的工程里面。
当你编译你的工程时,不会编译你调用的dll,当你运行到调用dll时,此时才会去调用dll,判断是否有语法语义,等编译,运行的错误。
这样利用反射具有一定灵活性,就是不用从你的工程调用dll,还有就是dll可随时改变(当然接口还是要对应),不需改变你所建的工程。
总之反射最好的好处就是新建工程时后不须一定要写好dll,可新建完工程后,后期写dll也可以。即所谓后期绑定。当然利用反射是耗资源的,损失效率,如果不是在上面的场合利用此技术,可能不会带来好处,反而是坏处。
本文来自CSDN博客,转载请标明出处:http://blog.csdn.net/boby16/archive/2009/07/30/4393422.aspx