Problem – I am trying my hand at plugIn development and all is going well except where trying to cast object A to object B even though A implements B.
Interface:
namespace DynamicApplications
{
public interface IPlugIn
{
string Name { get; set; }
IPlugInHost myHost { set; }
void Show();
}
public interface IPlugInHost
{
bool Register(IPlugIn plug);
}
}
Class which Implements IPlugIn
namespace plugInOne
{
class PlugIn : IPlugIn
{
IPlugInHost _myHost;
public string Name
{
get
{
return "Plug-In One";
}
set
{
}
}
public IPlugInHost myHost
{
set
{
_myHost = value;
}
}
public void Show()
{
}
}
}
And now THE CODE for the instantiation:
String path = Application.StartupPath;
string[] assemblyNames = Directory.GetFiles(path, "*.dll");
plugs = new IPlugIn[assemblyNames.Length];
for(int i = 0; i < assemblyNames.Length; i++)
{
string Name = assemblyNames[i];
Name = Name.Substring(Name.LastIndexOf("\\") + 1, Name.Length - Name.LastIndexOf("\\") - 1);
Name = Name.Remove(Name.LastIndexOf(".dll"));
assemblyNames[i] = Name;
}
for (int i = 0; i < assemblyNames.Length; i++)
{
Assembly DLL = Assembly.Load(assemblyNames[i]);
if(DLL != null)
{
try
{
Object p = Activator.CreateInstance(DLL.GetType(assemblyNames[i] + ".PlugIn"));
if (p is DynamicApplications.IPlugIn)
{
MessageBox.Show("YES!!!!");
}
else
{
MessageBox.Show("no>?>?>>><<?????");
}
plugs[i] = (IPlugIn)p;
}
catch(Exception ex)
{
MessageBox.Show(ex.Message);
}
}
}
Note the debugger shows that p is in fact instantiated and accessible
The Application always hits MessageBox.Show("no>?>?>>><<?????");
Help Please
Aiden
EDIT
P is of Type:

Also

YET plugInOne.PlugIn implements IPlugIn
Your referencing a
DynamicApplications.IPlugInand that is not the same interface which will be contained in the assembly.You need to get your
IPlugininstance from your assembly instead of using a local instance. Your code for checking is fine, if you did something like this:However you are getting your
A(in the above context) from somewhere else, and still trying to reference your local version ofIA– therefore you need to get your interface from your assembly instead of trying to reference it locally. Perhaps you could do something like this: