如何切换 .NET 程序集来执行一种方法?

发布于 2024-08-18 21:03:26 字数 295 浏览 9 评论 0原文

我的 .NET 应用程序有不同版本的 dll,大多数时候我想使用最新的版本。但是,有一种方法在单独的线程上运行,我需要能够根据某些条件选择旧版本的 dll。

我了解到,不可能只加载程序集,然后在默认应用程序域中卸载它(我不能只保留加载两个版本,因为这样我就会遇到类型的重复定义问题)

可能我必须创建一个单独的AppDomain,在那里加载程序集然后卸载它。该应用程序域将在单独的线程上仅执行一种方法,并可使用不同版本的库。

您认为这是一个好方法/您有更好的想法/您能给我指出一些可以让我开始的来源吗?

多谢 ;)

I have different versions of dlls for my .NET application and most of the time I want to use the latest one. However, there is one method which I run on a separate thread where I need to be able to select an older version of the dll based on some criteria.

I have learned that it is not possible to just load an assembly and then unload it within the default application domain (I can't just keep both versions loaded because then I'm running into duplicate definitions of types problem)

Probably I have to create a separate AppDomain, load the assembly there and then unload it. This application domain would execute just one method on a separate thread and would work with a different version of the library.

Do you think it is a good approach / have you better ideas / can you point me to some source which would get me started ?

Thanks a lot ;)

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

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

发布评论

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

评论(3

世界如花海般美丽 2024-08-25 21:03:26

尝试这样的事情:

class Program
{
    static void Main(string[] args)
    {
        System.Type activator = typeof(ApplicationProxy);
        AppDomain domain = 
            AppDomain.CreateDomain(
                "friendly name", null,
                new AppDomainSetup()
                {
                    ApplicationName = "application name"
                });

        ApplicationProxy proxy = 
            domain.CreateInstanceAndUnwrap(
                Assembly.GetAssembly(activator).FullName,
                activator.ToString()) as ApplicationProxy;

        proxy.DoSomething();

        AppDomain.Unload(domain);
    }
}

并创建一个代理类(必须继承自 MarshalByRefObject

class ApplicationProxy : MarshalByRefObject
{
    public void DoSomething()
    {
        Assembly oldVersion = Assembly.Load(new AssemblyName()
        {
            CodeBase = @"c:\yourfullpath\AssemblyFile.dll"
        });

        Type yourOldClass = oldVersion.GetType("namespace.class");
        // this is an example: your need to correctly define parameters below
        yourOldClass.InvokeMember("OldMethod", 
                                   BindingFlags.Public, null, null, null);
    }
}

Try something like this:

class Program
{
    static void Main(string[] args)
    {
        System.Type activator = typeof(ApplicationProxy);
        AppDomain domain = 
            AppDomain.CreateDomain(
                "friendly name", null,
                new AppDomainSetup()
                {
                    ApplicationName = "application name"
                });

        ApplicationProxy proxy = 
            domain.CreateInstanceAndUnwrap(
                Assembly.GetAssembly(activator).FullName,
                activator.ToString()) as ApplicationProxy;

        proxy.DoSomething();

        AppDomain.Unload(domain);
    }
}

And create a proxy class (must inherit from MarshalByRefObject)

class ApplicationProxy : MarshalByRefObject
{
    public void DoSomething()
    {
        Assembly oldVersion = Assembly.Load(new AssemblyName()
        {
            CodeBase = @"c:\yourfullpath\AssemblyFile.dll"
        });

        Type yourOldClass = oldVersion.GetType("namespace.class");
        // this is an example: your need to correctly define parameters below
        yourOldClass.InvokeMember("OldMethod", 
                                   BindingFlags.Public, null, null, null);
    }
}
倦话 2024-08-25 21:03:26

为什么不重写新的库,以在不同的方法中使用旧版本的代码并在需要时调用?

public void MyNewMethod(){}

public void MyLegacyMethod(){}

Why not rewrite you new library to have the older verion of the code in a different method and call when needed?

public void MyNewMethod(){}

public void MyLegacyMethod(){}
鹿港小镇 2024-08-25 21:03:26

也许您可以使用外部别名,请参阅 what-use-is-the-aliases-property-of- assembly-references-in-visual-studio-8

您应该能够为程序集的两个版本指定别名,并使用您的代码中有相同的前缀。

Purhaps you could use extern alias, see what-use-is-the-aliases-property-of-assembly-references-in-visual-studio-8

You ought to be able to specify an alias for the two versions of the assembly, and use same prefix's in your code.

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