dll动态加载到我的程序的问题 [英] Problem with dynamic loading of a dll into my program

查看:150
本文介绍了dll动态加载到我的程序的问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图添加插件到我的程序,这看起来不错,除了我不能从dll转换正确的类型。
我有一个解决方案与几个项目。
其中一个项目是一个国家层,实际上持有一个CountryBase(定义为public abstract class CountryBase:CountryLayers.ICountryBase)
接口(public interface ICountryBase)

I'm trying to add plugins to my program, and this looks good, except that I can't cast the correct type from the dll. I Have a solution with several projects on it. One of the project is a country Layer, that actually holds a CountryBase (defined as public abstract class CountryBase : CountryLayers.ICountryBase ) The Interface (public interface ICountryBase)

在另一个项目我有实施国家。此dll在运行时加载,使用:

On Another project I have the "Implementation" for the country. This dll is loaded at run time, using this:

            Assembly assembly = Assembly.LoadFrom(file);
            //get the class from the assembly
            foreach (Type t in assembly.GetTypes())
            {
//just for debugging
                Console.WriteLine(t.FullName);
            }

            Type localType = assembly.GetType( "CountryLayers.Local");
            if (localType != null)
            {
                Country countrydata = new Country();
                countrydata.ObjectType = localType;
                countrydata.CountryObject  = Activator.CreateInstance(localType);
                countrydata.CountryObject2 = (CountryBase) countrydata.CountryObject;
                countrydata.FileName = file;
                CountryList.Add(countrydata);
            }

其中Local是定义为public class的类的名称Local: CountryLayers.CountryBase,CountryLayers.ICountryBase

Where Local is the name of the class that is defined as public class Local : CountryLayers.CountryBase, CountryLayers.ICountryBase

countrydata只保留指针。 CountryObject2被定义为CountryBase(我也试过了IcountryBase)。但它总是返回类型不可转换。

countrydata just holds pointer. CountryObject2 is defined as CountryBase (I also tried as IcountryBase). But It always returned that the type is not convertible.

控制台writeline显示,在程序集中加载了属于countrylayer和本地类的所有类。

The console writeline showed that in the assembly are loaded all the classes that belongs to the countrylayer and also the local class.

所以在这一点上我不知道错误是因为我有一切都在同一个解决方案,或者问题是,我使用的接口和抽象类在坏的顺序。此外,当create instance返回对象时,该对象具有抽象类中定义的所有属性,但没有方法。

So At this point I don't know if the error is because I have everything on the same solution, or the problem is that I'm using the interface and the abstract class in a bad order. Also when create instance returns the object, that object has all the properties defined in the abstract class, but no method.

推荐答案

解决它自己。使用解决方案编译所有内容时会出现问题。这样,它为接口也在其他类的输出中创建dll。考虑到这一点,我使用一个插件文件夹来保留插件,并设置VS编译输出到该文件夹​​。它将派生类复制到该文件夹​​,以及接口dll和抽象实现dll。所以它使编译器困惑。我拿出VS中每个项目的引用(我改变它们引用编译的dll),我自己编译每个dll,然后我只复制我需要的dll到插件文件夹,并工作。
反正这个链接给了我一个关于这个问题的提示:
动态使用反射加载

I solve it myself. The problem arises when you compile everything using the solution. That way, it creates dll for the interfaces also in the output of the other classes. With that in mind, I was using a plugin folder to keep the plugins, and set the VS to compile the output to that folder. It copy the derived class to that folder, along with the interface dll and the abstract implementation dll. So it made the compiler confused. I Take out every reference for projects in the VS (I change them to reference to the "compiled" dll), I compile every dll by itself, and then I only copy the dll I needed to the plugin folder, and worked. Anyway this link gave me a tip about the problem: Dynamic Loading with Reflection

这篇关于dll动态加载到我的程序的问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆