首页 新闻 会员 周边

CreateInstanceAndUnwrap

0
悬赏园豆:50 [已关闭问题] 关闭于 2010-03-09 20:08

我做了一个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'.

请问大家有什么好办法吗?

棋圣的主页 棋圣 | 初学一级 | 园豆:197
提问于:2010-02-23 09:12
< >
分享
所有回答(3)
-2

好像不能这么用,参考一下代码给你一些启示

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();
        }
    }

查尔斯 | 园豆:3832 (老鸟四级) | 2010-02-23 18:28
这个上次你发给我了,后来我调试明白了,在winform程序里没问题,放在插件里就出问题啊。
支持(0) 反对(0) 棋圣 | 园豆:197 (初学一级) | 2010-02-23 20:03
应该不会,如果不能用,那么问题应该不会在这里。 上面报的错就是透明代理的类型转换问题
支持(0) 反对(0) 查尔斯 | 园豆:3832 (老鸟四级) | 2010-02-23 23:34
问题不在代码,而是因为做的是 vs2005的插件啊 你可以自己尝试一下就可以发现我说的问题
支持(0) 反对(0) 棋圣 | 园豆:197 (初学一级) | 2010-02-24 08:32
0

问题怎么解决的?我也一样碰上这个问题了。

heguo | 园豆:201 (菜鸟二级) | 2015-01-27 13:59
0

问题解决了吗?我在做插件的时候也遇到这个问题了

狂风逆袭 | 园豆:202 (菜鸟二级) | 2016-10-26 10:49
清除回答草稿
   您需要登录以后才能回答,未注册用户请先注册