在另一个 AppDomain 中创建类型的实例

发布于 2024-08-18 05:32:40 字数 986 浏览 8 评论 0原文

我的场景是,我有一个创建 AppDomain 的 .net 应用程序(假设是控制台应用程序)。然后,它需要创建实例并调用该 AppDomain 中类型的方法。每个AppDomain都有一个特定的目录,它的依赖项应该在其中,该目录不在控制台应用程序目录下(甚至不在控制台应用程序目录附近)。这是我的简单代码:

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显示它搜索的目录是控制台应用程序目录。它不包括从 AppDomain 设置的搜索文件夹 - 在“baseDirecory”变量中。

我做错了什么?是否有另一种方法可以执行位于另一个 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" ); 

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.

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

Thanks...

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

听不够的曲调 2024-08-25 05:32:40

一种解决方法是将完整路径传递给 .CreateInstanceFrom 调用:

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

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" );
趁微风不噪 2024-08-25 05:32:40

我不能说你的代码有什么问题,因为它看起来应该可以工作。然而,当我完成这件事后,我就找了一个帮手来完成这项工作。大致是这样的:

public class Loader
{
    public void Load(string typename)
    {
        // ....
    }
}

Loader l = (Loader)domain.CreateInstanceAndUnwrap("Loader");
l.Load("SomeClassInTheDll");

I can't say what's wrong with your code, as it looks like it should work. When I have doen this, however, I've made a helper to do the work. Roughly like this:

public class Loader
{
    public void Load(string typename)
    {
        // ....
    }
}

Loader l = (Loader)domain.CreateInstanceAndUnwrap("Loader");
l.Load("SomeClassInTheDll");
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文