我做了一个vs2005的addins,在里面使用了
using System;
using System.Collections.Generic;
using System.Text;
using System.IO;
using System.Reflection;
using System.Security;
using CodeEngine;
using SchemaExplorer;
namespace DataBase
{
public class LocalLoader
{
private AppDomain appDomain;
private RemoteLoader remoteLoader;
public LocalLoader()
{
PermissionSet perSet = new System.Security.PermissionSet(System.Security.Permissions.PermissionState.Unrestricted);
AppDomainSetup setup = new AppDomainSetup();
setup.ApplicationName = "Test";
//setup.ApplicationBase = AppDomain.CurrentDomain.BaseDirectory;
setup.ApplicationBase = new Common().ApplicationPath;
setup.PrivateBinPath = "bin;temp;debug";
//setup.CachePath = setup.ApplicationBase;
//setup.ShadowCopyFiles = "true";
//setup.ShadowCopyDirectories = setup.ApplicationBase;
appDomain = AppDomain.CreateDomain("TestDomain", null, setup, perSet, null);
try
{
remoteLoader = CreateInstance<RemoteLoader>();
}
catch (Exception ex)
{
}
}
public T CreateInstance<T>()
{
return (T)this.appDomain.CreateInstanceAndUnwrap(typeof(T).Assembly.FullName, typeof(T).FullName);
}
public StringBuilder LoadAssembly(string fullName, string className, TableSchema table)
{
return remoteLoader.Create(fullName, className, table);
}
public void Unload()
{
AppDomain.Unload(appDomain);
appDomain = null;
}
}
}
由于是在插件里运行的,在运行下面代码时候出现错误
return (T)this.appDomain.CreateInstanceAndUnwrap(typeof(T).Assembly.FullName, typeof(T).FullName);
错误信息如下:
Unable to cast transparent proxy to type 'DataBase.RemoteLoader'.
请问大家有什么好办法吗?
好像不能这么用,参考一下代码给你一些启示
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);
public interface IRemoteInterface
{
object Invoke(string lcMethod, object[] Parameters);
}
/// <summary>
/// Factory class to create objects exposing IRemoteInterface
/// </summary>
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();
}
}
问题怎么解决的?我也一样碰上这个问题了。
问题解决了吗?我在做插件的时候也遇到这个问题了