Casting an object into the base interface

I had a problem with casting an object to one of its basic interfaces living in another library. Here is the code for it:

BaseSDK.dll

public interface IPlugin
{
    void Run();
}

CustomPlugin.Definition.dll:

public interface ICustomPlugin
{
    void DoCustomStuff();
}

CustomPlugin.dll (has a link to BaseSDK.dll and CustomPlugin.Definition.dll):

public class CustomPlugin: IPlugin, ICustomPlugin
{
    public void Run()
    {

    }

    public void DoCustomStuff()
    {

    }
}

Host.exe (has links to BaseSDK.dll and CustomPlugin.Definition.dll):

IPlugin plugin;
public void DoStuff()
{
    plugin = LoadPluginAndCreateAnInstanceSomehow();
    // I know plugin is a CustomPlugin
    ICustomPlugin customPlugin = plugin as ICustomPlugin; //cast fails.
    customPlugin.DoCustomStuff();
}

I do not understand; it's just a simple type-casting of a base type to it. How can i fix this? or any alternatives?

Edit: The following is a brief description of what it LoadPluginAndCreateAnInstanceSomehow()does:

Assembly ass = Assembly.LoadFrom(filename);
Type t = ass.GetType(ass.FullName + ".CustomPlugin");
plugin = (IPlugin)Activator.CreateInstance(t);

2: AppDomain. IPlugin ICustomPlugin.Definiton , , CustomPlugin. ? , , ?

3: :

   public class PluginLoader
    {
        List<IPlugin> Plugins;
        AppDomain Domain;
        string libPath;
        public void PluginLoader(string sLibPath, AppDomain sDomain)
        {

            libPath = sLibPath;
            Plugins = new List<IPlugin>();
            Domain = sDomain;
            if(Domain==null)Domain = AppDomain.CreateDomain("PluginsDomain");
            Domain.AssemblyResolve += new ResolveEventHandler(Domain_AssemblyResolve);
            Domain.TypeResolve += new ResolveEventHandler(Domain_TypeResolve);
            Domain.DoCallBack(new CrossAppDomainDelegate(DomainCallback));
        }

        Assembly Domain_AssemblyResolve(object sender, ResolveEventArgs args)
        {
            return Assembly.LoadFrom(args.Name);
        }
        Assembly Domain_TypeResolve(object sender, ResolveEventArgs args)
        {
            return Assembly.LoadFrom(args.Name);
        }
        void DomainCallback()
        {
            string[] fileNames = Directory.GetFiles(libPath, "*.dll");
            foreach (string filename in fileNames)
            {

                FileInfo fi = new FileInfo(filename);
                Assembly ass = Assembly.LoadFrom(fi.FullName);
                Type t = ass.GetType(ass.FullName + ".CustomPlugin");
                IPlugin p = (IPlugin)Activator.CreateInstance(t);
                Plugins.Add(p);
            }
        }
    }
+5
3

. .

, (, )

C:\Runtime\- Host.exe
C:\Runtime\Library\-

exe , , , , CustomPlugin, .

, DID CustomPlugin.dll \Runtime? . , .

, AppDomain \Library, , AppDomain, AppDomain , . . ICustomConfig, . , , .NET !

,

host.exe
BaseSDK.dll
CustomPlugin.Definition.dll
CustomPlugin.dll - , ICustomPlugin

BaseSDK.dll
CustomPlugin.Definition.dll
CustomPlugin.dll -

plugin.GetType(). Assembly.Location, , DLL AppDomain.

+2

, , "" .

MSDN : " as , , , null ".

:

ICustomPlugin customPlugin = (ICustomPlugin)plugin;

, - , .

+1

, ICustomPlugin. , , , CustomPlugin.dll CustomPlugin.Definition.dll, Host.exe.

0

All Articles