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.