插件应用程序出现转换错误

发布于 2024-12-23 18:58:08 字数 1299 浏览 2 评论 0原文

我创建了一个控制台应用程序,它搜索以 PlugIn.dll 结尾的插件。 它加载dll程序集并执行PlugIn.dll中plugInClass的write方法。 我创建了一个名为 IWrite 的接口,其中包含 write 方法。 执行控制台应用程序后,出现如下错误:

无法将类型为“HPlugIn.plugInClass”的对象转换为类型为“ConsolePlugIn.IWrite”。

这是我的控制台应用程序的代码..[主应用程序]

using System;
using System.IO;
using System.Reflection;
namespace ConsolePlugIn
{
    interface IWrite
    {
        void write();
    }
    class Program
    {
        static void Main(string[] args)
        {
            foreach (string s in Directory.GetFiles(AppDomain.CurrentDomain.BaseDirectory, "*PlugIn.dll"))//getting plugins in base directory
            {
                Assembly aWrite = Assembly.LoadFrom(s);
                Type tWrite = aWrite.GetType("HPlugIn.plugInClass");
                IWrite click = (IWrite)Activator.CreateInstance(tWrite);//giving casting error
                click.write();
            }
        } 
    }
}

这是我的插件 dll 文件的代码

using System;
namespace HPlugIn
{
    interface IWrite
    {
        void write();
    }
    public class plugInClass : IWrite
    {
      public  void write()
        {
            Console.Write("High from plugInClass");
        }
    }
}

对这个转换错误有什么想法吗? 提前致谢!

I created a Console application which searches for plugins ending with PlugIn.dll.
It loads the dll assembly and executes the write method of plugInClass in PlugIn.dll.
I created an interface called IWrite which includes the write method.
After executing the console app,it gives an error as given below:

Unable to cast object of type 'HPlugIn.plugInClass' to type 'ConsolePlugIn.IWrite'.

Here is my code for console app..[Main application]

using System;
using System.IO;
using System.Reflection;
namespace ConsolePlugIn
{
    interface IWrite
    {
        void write();
    }
    class Program
    {
        static void Main(string[] args)
        {
            foreach (string s in Directory.GetFiles(AppDomain.CurrentDomain.BaseDirectory, "*PlugIn.dll"))//getting plugins in base directory
            {
                Assembly aWrite = Assembly.LoadFrom(s);
                Type tWrite = aWrite.GetType("HPlugIn.plugInClass");
                IWrite click = (IWrite)Activator.CreateInstance(tWrite);//giving casting error
                click.write();
            }
        } 
    }
}

here is my code for the plugIn dll file

using System;
namespace HPlugIn
{
    interface IWrite
    {
        void write();
    }
    public class plugInClass : IWrite
    {
      public  void write()
        {
            Console.Write("High from plugInClass");
        }
    }
}

Any idea for this casting error?
Thanks in advance!

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

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

发布评论

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

评论(3

梦回旧景 2024-12-30 18:58:08

EXE 和 DLL 中的 IWrite 接口并不相同,尽管它们的结构相同。您需要使用该接口创建第三个 dll,并在 DLL 和 EXE 之间共享它。

常见:

namespace Shared {
    interface IWrite {
        void write();
    }
}

DLL:

using System;
using Shared;
namespace HPlugIn {
    public class plugInClass : IWrite {
        public  void write() {
            Console.Write("High from plugInClass");
        }
    }
}

EXE:

using System;
using System.IO;
using System.Reflection;
using Shared;
namespace ConsolePlugIn {
    class Program {
        ...
    } 
}

The IWrite interfaces in the EXE and in the DLL are not the same, even though their structures are identical. You need to make a third dll with the interface, and share it among the DLLs and the EXE.

Common:

namespace Shared {
    interface IWrite {
        void write();
    }
}

DLL:

using System;
using Shared;
namespace HPlugIn {
    public class plugInClass : IWrite {
        public  void write() {
            Console.Write("High from plugInClass");
        }
    }
}

EXE:

using System;
using System.IO;
using System.Reflection;
using Shared;
namespace ConsolePlugIn {
    class Program {
        ...
    } 
}
困倦 2024-12-30 18:58:08

您已在 2 个不同的程序集中定义了两次 IWrite 接口。它们被视为不同的类型,您不能从一种类型转换为另一种类型。在 EXE 和程序集之间实现弱耦合的最佳方法是将此接口定义到单独的 DLL 中。然后让插件和可执行文件都引用包含合约的第三个程序集(IWrite 接口)。

You have defined the IWrite interface twice in 2 different assemblies. They are considered different types and you cannot cast from one to the other. The best way to achieve weaker coupling between the EXE and the assembly is to define this interface into a separate DLL. Then have the plugin and executable both reference this third assembly containing the contract (the IWrite interface).

温柔戏命师 2024-12-30 18:58:08

有两个不同的 IWrite 接口。一份在控制台应用程序中,一份在 dll 中。有两种方法可以解决这个问题。

  • 使 dll 引用控制台应用程序并让 plugInClass 实现 ConsolePlugIn.IWrite 接口。
  • 使用 dynamic 关键字使界面看起来相同。

There are two different IWrite interfaces. One in the console app and one in the dll. There are two ways to work around this.

  • Make the dll reference the console app and have plugInClass implement the ConsolePlugIn.IWrite interface.
  • Use the dynamic keyword to make interfaces looking the same match.
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文