Problem with dynamic loading of a dll into my program

浪尽此生 提交于 2019-12-14 02:15:44

问题


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)

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);
            }

Where Local is the name of the class that is defined as public class Local : CountryLayers.CountryBase, CountryLayers.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.

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.


回答1:


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




回答2:


I experienced a similar situation in the past, when the dll that you loaded dynamically referenced a different version of the dll containing the base class (CountryBase in your case I guess).



来源:https://stackoverflow.com/questions/835551/problem-with-dynamic-loading-of-a-dll-into-my-program

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!