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

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

问题描述

我想要的插件添加到我的程序,这看起来不错,但我不能从DLL投下正确的类型。
我有与它的几个项目的解决方案。该项目的
一个是国家层,实际持有CountryBase(定义为公共抽象类CountryBase:CountryLayers.ICountryBase)
接口(公共接口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);
            }



如果当地是被定义为公共类本地的类的名称: 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.

所以,在这一点上,我不知道如果错误是因为我有同一个解决方案的一切,或者问题是,我使用的接口和抽象类在一个坏的顺序。此外,当创建实例,则返回的对象,这个对象在抽象类中定义的所有特性,但没有一种方法。

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天全站免登陆