I created a Console application which searches for plugins ending with PlugIn.dll.
It loads the dll assembly and executes the write method of plugInClass in PlugIn.dll.
I created an interface called IWrite which includes the write method.
After executing the console app,it gives an error as given below:
Unable to cast object of type ‘HPlugIn.plugInClass’ to type ‘ConsolePlugIn.IWrite’.
Here is my code for console app..[Main application]
using System;
using System.IO;
using System.Reflection;
namespace ConsolePlugIn
{
interface IWrite
{
void write();
}
class Program
{
static void Main(string[] args)
{
foreach (string s in Directory.GetFiles(AppDomain.CurrentDomain.BaseDirectory, "*PlugIn.dll"))//getting plugins in base directory
{
Assembly aWrite = Assembly.LoadFrom(s);
Type tWrite = aWrite.GetType("HPlugIn.plugInClass");
IWrite click = (IWrite)Activator.CreateInstance(tWrite);//giving casting error
click.write();
}
}
}
}
here is my code for the plugIn dll file
using System;
namespace HPlugIn
{
interface IWrite
{
void write();
}
public class plugInClass : IWrite
{
public void write()
{
Console.Write("High from plugInClass");
}
}
}
Any idea for this casting error?
Thanks in advance!
The
IWriteinterfaces in the EXE and in the DLL are not the same, even though their structures are identical. You need to make a third dll with the interface, and share it among the DLLs and the EXE.Common:
DLL:
EXE: