在另一个AppDomain中一类的CreateInstance [英] CreateInstance of a Type in another AppDomain

查看:356
本文介绍了在另一个AppDomain中一类的CreateInstance的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的情况是,我有一个.NET应用程序(比方说,一个控制台应用程序)创建的AppDomain。然后,它需要创建实例,并呼吁那些在AppDomain中类型的方法。每个AppDomain中有哪里是依赖条件应该是一个特定的目录,这是不(甚至接近)控制台应用程序目录下。这里是我的简单的代码:

My scenario is that I have a .net application (let's say a Console App) that creates AppDomains. It then needs to create instances and call methods on Types that are in that AppDomain. Each AppDomain has a specific directory where are it's dependecies should be, which is not under (or even near) the Console Apps directory. Here's my simple code:

string baseDirectory = "c:\foo"; // <- where AppDomain's dependecies 

// set up the app domain
AppDomainSetup setup = new AppDomainSetup();
setup.ApplicationName = DateTime.Now.ToString("hh:MM:ss:ffff");
setup.ApplicationBase = baseDirectory;
setup.PrivateBinPath = baseDirectory;

// create app domain
AppDomain domain = AppDomain.CreateDomain(
    name,
    AppDomain.CurrentDomain.Evidence,
    setup );

// instantiate Type from an assembly in that AppDomain
ObjectHandle handle = domain.CreateInstanceFrom(
    "SampleClassLibrary.dll", // <- exists in "c:\foo" 
    "SomeClassInTheDll" ); 

要CreateInstanceFrom结果在FileNotFoundExcepotion呼叫。的FusionLog表明目录它searchedwere控制台应用程序的目录。它不包括分别设置在该AppDomain中搜索文件夹 - 在baseDirecory变量

The call to CreateInstanceFrom results in a FileNotFoundExcepotion. The FusionLog shows that the directories it searchedwere the Console applications directories. It did not include search folders that were set from the AppDomain - in the "baseDirecory" variable.

我在做什么错了?有另一种方式来执行,生活在另一个AppDomain中的代码?

What am I doing wrong? Is there another way to execute code that lives in another AppDomain?

谢谢...

推荐答案

一个解决方法是将完整路径传递给.CreateInstanceFrom电话:

One workaround would be to pass the full path to the .CreateInstanceFrom call:

ObjectHandle handle = domain.CreateInstanceFrom( 
    baseDirectory + @"\SampleClassLibrary.dll", // <- exists in "c:\foo"  
    "SomeClassInTheDll" );

这篇关于在另一个AppDomain中一类的CreateInstance的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆