· 创建另外一个Application Domain
· 动态创建(编译)代码并保存到磁盘
· 创建一个公共的远程调用接口
· 创建远程调用接口的实例并通过这个接口来访问其方法
换句话来讲就是将对象加载到另外一个AppDomain中并通过远程调用的方法来调用所谓远程调用其实也就是跨应用程序域调用所以这个对象(动态代码)必须继承于MarshalByRefObject类为了复用这个接口被单独提到一个工程中并提供一个工厂来简化每次的调用操作
using System;
using SystemCollectionsGeneric;
using SystemLinq;
using SystemText;
using SystemReflection;
namespace RemoteAccess
{
///
/// Interface that can be run over the remote AppDomain boundary
///
public interface IRemoteInterface
{
object Invoke(string lcMethodobject[] Parameters);
}
///
/// Factory class to create objects exposing IRemoteInterface
///
public class RemoteLoaderFactory : MarshalByRefObject
{
private const BindingFlags bfi = BindingFlags Instance | BindingFlagsPublic | BindingFlagsCreateInstance;
public RemoteLoaderFactory() {}
public IRemoteInterface Create( string assemblyFile string typeName object[] constructArgs )
{
return (IRemoteInterface) ActivatorCreateInstanceFrom(
assemblyFile typeName false bfi null constructArgs
null null null )Unwrap();
}
}
}
[] [] [] [] []