一、使用System.Activator类
System.Activator提供了两个静态方法:createinstance()和createinstancefrom(),如果包含类的程序集未出现在Appdomain中,调用:createinstance()和createinstancefrom()会导致该程序集被载入。
using System;
using System.Reflection;
namespace ConsoleApplication5
{
classProgram
{
staticvoid
{
Assemblyassembly = Assembly.Load("Foo.dll");
Typetype = assembly.GetType("NMFoo.Calc");
objectobj = Activator.CreateInstance(type);
}
}
}
二、 使用System.Appdomain。与之前的Activator.CreateInstance类似,不过通过他们可以选择将对象创建在哪个Appdomain中,此外“AndUnwarp()”版本会返回一个对对象的直接引用。
using System;
using System.Reflection;
class Program {
staticvoid
objectobj = AppDomain.CurrentDomain.CreateInstanceAndUnwrap(
"Foo.dll", "NMFoo.Calc");
}
}
三、使用System.Reflecton.ConstructorInfo类,该类的invoke()方法在内部为构造函数创建了一个后期绑定,并通过这个绑定调用构造函数
using System;
using System.Reflection;
namespace ConsoleApplication5
{
classProgram
{
staticvoid
{
Assemblyassembly = Assembly.LoadFrom(@"C:\Foo.dll");
Typetype = assembly.GetType("NMFoo.Calc");
ConstructorInfoconstructorInfo = type.GetConstructor(new Type[0]);
objectobj = constructorInfo.Invoke(new object[0]);
}
}
}
四、 使用System.Type类
通过System.Type类的非静态方法Invokemember()可以创建一个在编译期间未知的类的实例,只需要在调用的时候使用BIndingFlages枚举量中的CreateInstance即可
using System;
using System.Reflection;
namespace ConsoleApplication5
{
classProgram
{
staticvoid
{
Assemblyassembly = Assembly.LoadFrom(@"C:\Foo.dll");
Typetype = assembly.GetType("NMFoo.Calc");
Objectobj = type.InvokeMember(
null, // Don't need toprovide a name for calling a constructor.
BindingFlags.CreateInstance,
null, // Don't need abinder.
null, // Don't need atarget object since we build it.
new Object[0]); // No parameters.
//Here, 'obj' is a reference toward an instance of NMFoo.Calc.
}
}
}
五、对于数组和委托对象的实例
为创建一个数组,必须调用System.Array类中的静态方法Createinstance()
为创建一个委托对象,必须调用System.Delegate类中的CreateDelegate()方法
六、一次绑定,多次调用
using System;
using System.Reflection;
namespace ConsoleApplication5
{
classProgram
{
staticvoid
{
objectobj = AppDomain.CurrentDomain.CreateInstanceAndUnwrap(
"Foo.dll", "NMFoo.Calc");
Typetype = obj.GetType();
//Create a late bind with the 'sum'?method.
MethodInfomethodInfo = type.GetMethod("Sum");
object[]parameters = new object[2];
parameters[0] = 7;
parameters[1] = 8;
intresult;
//10 calls to 'Sums'
//for (int i = 0;i < 10; i++)
result = (int)methodInfo.Invoke(obj, parameters);
Console.WriteLine(Convert.ToString(result));
}
}
}
七、利用接口:使用后期绑定的正确方法
1、 创建包含接口的程序集的代码(InterfaceAsm.CS)
using System;
using System.Collections.Generic;
using System.Text;
namespace NMFoo
{
publicinterface ICalc
{
intSum(int a, intb);
}
}
2、 创建目标类的程序集的代码(ClassAsm.CS)
using System;
using System.Collections.Generic;
using System.Text;
namespace NMFoo
{
publicclass CalcWithInterface:ICalc
{
publicCalcWithInterface()
{
Console.WriteLine("Calc.constructor called!");
}
publicint Sum(int a, int b)
{
Console.WriteLine("method Calc.Sum() Called!");
returna + b;
}
}
}
3、 在编译期目标类未知的客户程序集的代码(ProgramAsm.CS)
using System;
using System.Reflection;
using NMFoo;
using System.IO;
namespace
{
classProgram
{
staticvoid
{
//Console.WriteLine(System.Environment.CurrentDirectory);
ICalcobj = AppDomain.CurrentDomain.CreateInstanceAndUnwrap("ClassAsm.dll", "NMFoo.CalcWithInterface")as ICalc;
//intresult = obj.Sum(7, 8);
Console.WriteLine(System.Environment.CurrentDirectory);
}
}
}
4、