views:

188

answers:

2

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.

A: 

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).

Grzenio
But I have everything on the same solution now, for debugging and testing.. and is not working.. (same solution, different project for each of them)...
gbianchi
+1  A: 

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

gbianchi