C# 错误?使用通用类型加载组件

发布于 2024-11-15 22:45:42 字数 1202 浏览 0 评论 0原文

我有一个像这样动态加载程序集的类:

    Assembly asm = Assembly.LoadFile(args[1]);        
    runner.RunTestOnAssembly(asm);

然后另一个类在该程序集上运行测试:

    foreach (var cspecType in asm.GetTypes())
    {
        RunTestOnType(cspecType);
    }

加载的程序集引用同一文件夹中的其他程序集,[Debug\Tests]以及加载的程序该程序集位于[调试]文件夹中。

加载的程序集 (CalcSpecAsm):

    public class CalcSpec : CSpecFacade<ICalc>
    {
        public CalcSpec()
        : base(new Calc())
        {

        }
    }

引用的程序集 (CalcAsm):

    public class Calc : ICalc
    {
    /// <summary>
    /// Initalisation constructor
    /// resets the Total value.
    /// </summary>
    public Calc()
    {
        Total = 0;
    }
    .....

CSpecFacade 在另一个程序集中引用。

现在的问题是,如果我加载程序集 CalcSpecAsm,我会在 GetTypes() 上收到程序集加载器错误,它无法解析对 CalcAsm 程序集的引用。

问题是这行代码: public class CalcSpec : CSpecFacade

如果我删除通用参数 ICalc 并在构造函数中初始化它,那么它可以完美工作,但这不是我想要的。

但是:如果我将 loaderAssembly 程序复制到 [Debug\Test] 然后运行它,一切正常。

如何解决这个问题,是什么原因造成的?

I have an class that loads the assembly dynamically like so:

    Assembly asm = Assembly.LoadFile(args[1]);        
    runner.RunTestOnAssembly(asm);

then another class runs the test on that assembly:

    foreach (var cspecType in asm.GetTypes())
    {
        RunTestOnType(cspecType);
    }

The loaded assembly references other assemblies in the same folder, [Debug\Tests] and the program that loads the assembly is in the [Debug] folder.

The loaded Assembly (CalcSpecAsm):

    public class CalcSpec : CSpecFacade<ICalc>
    {
        public CalcSpec()
        : base(new Calc())
        {

        }
    }

The referenced Assembly (CalcAsm):

    public class Calc : ICalc
    {
    /// <summary>
    /// Initalisation constructor
    /// resets the Total value.
    /// </summary>
    public Calc()
    {
        Total = 0;
    }
    .....

The CSpecFacade is referenced in another assembly.

And now the problem is that if I load the assembly CalcSpecAsm I get assembly loaderError on GetTypes() that it cannot resolve a reference to CalcAsm assembly.

The problem is this line of code: public class CalcSpec : CSpecFacade<ICalc>

If I remove the generic arg ICalc and initize it in the constructor then it works perfect but that's not what I'm after.

BUT: If i copy the loaderAssembly program to the [Debug\Test] and then run it, everything works fine.

How to resolve the problem, and what causes it?

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

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

发布评论

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

评论(1

失与倦" 2024-11-22 22:45:42

当您从其他位置加载程序集时,可能会收到错误消息,指出无法加载程序集或其依赖项。

这意味着在加载程序集时,.Net 运行时无法解析程序集的所有依赖项。

所以你需要提供一种方法来解决这个问题。

首先,您必须像这样将解析事件添加到您的应用程序域

AppDomain.CurrentDomain.AssemblyResolve += new ResolveEventHandler(CurrentDomain_AssemblyResolve);

,然后在这种情况下您将必须加载您的依赖项。

    Assembly CurrentDomain_AssemblyResolve(object sender, ResolveEventArgs args)
    {

        //Build the path of the assembly from where it has to be loaded.                
        assemblyPath = System.IO.Path.Combine("your path", args.Name.Substring(0, args.Name.IndexOf(",")) + ".dll");
        assembly = Assembly.LoadFrom(assemblyPath);

        //Return the loaded assembly.
        return assembly;
    }

希望这会有所帮助。

when you load assemblies from other locations, you might get errors saying that assembly could not be loaded or it's dependencies could not be loaded.

This means that when loading your assembly .Net Runtime was not able to resolve all dependencies of your assembly.

So you need to provide a way to resolve this.

First you will have to add resolve event to your appdomain like this

AppDomain.CurrentDomain.AssemblyResolve += new ResolveEventHandler(CurrentDomain_AssemblyResolve);

Then in that event you will have to load your dependencies.

    Assembly CurrentDomain_AssemblyResolve(object sender, ResolveEventArgs args)
    {

        //Build the path of the assembly from where it has to be loaded.                
        assemblyPath = System.IO.Path.Combine("your path", args.Name.Substring(0, args.Name.IndexOf(",")) + ".dll");
        assembly = Assembly.LoadFrom(assemblyPath);

        //Return the loaded assembly.
        return assembly;
    }

Hope this will help.

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