简体   繁体   English

在运行时加载程序集 .NET 6

[英]Load Assembly at Runtime .NET 6

We are in the beginning stages of converting a c# Winforms App from .NET Framework to .NET 6. We can get the project to build and run in .NET 6, but when it comes to a dynamically loaded assembly, we are having issues.我们正处于将 c# Winforms 应用程序从 .NET Framework 转换为 .NET 6 的开始阶段。我们可以让项目在 .NET 6 中构建和运行,但是当涉及到动态加载的程序集时,我们遇到了问题。 We can get the assembly to load but attempting to access the custom class within it returns a null. I recreated this scenario in two smaller projects as an example.我们可以加载程序集,但尝试访问其中的自定义 class 会返回 null。我在两个较小的项目中重新创建了这个场景作为示例。

Solution 1/Project 1 - The code for the assembly to be loaded into the main application.解决方案 1/项目 1 - 要加载到主应用程序中的程序集的代码。 This is a class library that creates the TestAssembly.dll这是创建 TestAssembly.dll 的 class 库

namespace Custom.TestAssembly
{
    public class TestClass : CallingModule
    {
        public override object GetValue()
        {
            return "Hello World";
        }

    }
}

Solution 2/Project 1 - This is a project and class within the main application's solution.解决方案 2/项目 1 - 这是主应用程序解决方案中的项目和 class。 This is a class library that creates the Custom.TestAssembly.dll这是创建 Custom.TestAssembly.dll 的 class 库

namespace Custom.TestAssembly
{
    public class CallingModule
    {
        public virtual object? GetValue()
        {
            return null;
        }
    }
}

Solution 2/Project 2 - A button has been placed on a form.解决方案 2/项目 2 - 在窗体上放置了一个按钮。 When it is clicked, the assembly should be loaded, which it is.单击它时,应该加载程序集,它是。 However, attempting to extract the class from the assembly always returns a NULL.但是,尝试从程序集中提取 class 总是会返回 NULL。

Form1.cs Form1.cs

using System.Collections.Generic;
using System.Diagnostics;
using System.Reflection;
using System.Reflection.Emit;
using System.Runtime.Loader;


namespace TestCallingApplication
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        private void button1_Click(object sender, EventArgs e)
        {
            Assembly dynamicAssembly = AssemblyLoadContext.Default.LoadFromAssemblyPath(@"C:\LocationOf\TestAssembly.dll");
            Module customizationModule = dynamicAssembly.GetModule("TestAssembly.dll");
            Type customClientModule = customizationModule.GetType("Custom.TestAssembly.TestClass");  //THIS RETURNS A NULL??

        }

    }
}

Just trying to understand what I am missing.只是想了解我所缺少的。 Any thoughts?有什么想法吗? Or a better way to load runtime assemblies and access classes within them in .NET 6?或者在 .NET 6 中加载运行时程序集并访问其中的类的更好方法?

Did you reference Solution 2/Project 1?您是否参考了解决方案 2/项目 1? Since they have the same assembly name Custom.TestAssembly , the runtime will not load it again if already loaded in memory.由于它们具有相同的程序集名称Custom.TestAssembly ,如果已在 memory 中加载,运行时将不会再次加载它。

You can, however, load it under a different AssemblyLoadContext , there's an example on MSDN as well.但是,您可以在不同的AssemblyLoadContext下加载它, MSDN 上也有一个示例

Also, you may want to take a look at DotNetCorePlugins , which takes care of assembly loading, reloading, isolation, shared type, and dependency resolving.此外,您可能想看看DotNetCorePlugins ,它负责程序集加载、重新加载、隔离、共享类型和依赖项解析。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM