1 回答
TA贡献1841条经验 获得超3个赞
请参考如下代码片段:
public DynamicAssembly()
{
PermissionSet perSet = new System.Security.PermissionSet(System.Security.Permissions.PermissionState.Unrestricted);
AppDomainSetup objSetup = new AppDomainSetup();
objSetup.ApplicationBase = AppDomain.CurrentDomain.BaseDirectory;
_objAppDomain = AppDomain.CreateDomain("MyAppDomain", null, objSetup, perSet, null);
}
public void InitCall()
{
string strErrorMsg = string.Empty;
RemoteLoaderFactory factory = (RemoteLoaderFactory)_objAppDomain.CreateInstance("UIT.DynamicExpression.RemoteAccess", "UIT.DynamicExpression.RemoteAccess.RemoteLoaderFactory").Unwrap();
// with help of factory, create a real 'LiveClass' instance
_object = factory.Create("UIT.DynamicalExpressionBin.dll", "UIT.DynamicExpression.Eval", null);
if (_object == null)
{
strErrorMsg = "Error: " + "Couldn't load class.";
Trace.WriteLine(strErrorMsg);
}
}
其中RemoteFactory类代码如下:
public class RemoteLoaderFactory : MarshalByRefObject
{
private const BindingFlags bfi = BindingFlags.Instance | BindingFlags.Public | BindingFlags.CreateInstance;
public RemoteLoaderFactory() { }
public IRemoteInterface Create(string assemblyFile, string typeName, object[] constructArgs)
{
return (IRemoteInterface)Activator.CreateInstanceFrom(
assemblyFile, typeName, false, bfi, null, constructArgs,
null, null, null).Unwrap();
}
}
- 1 回答
- 0 关注
- 384 浏览
添加回答
举报