Reputation: 917
I loaded and got an instance form a dll by this code:
ApiClass api = new ApiClass(this);
Assembly SampleAssembly = Assembly.LoadFrom(@"C:\plugin1.dll");
Type myType = SampleAssembly.GetTypes()[0];
MethodInfo Method = myType.GetMethod("onRun");
object myInstance = Activator.CreateInstance(myType);
try
{
object retVal = Method.Invoke(myInstance, new object[] { api });
}
and here is the code of IApi interface:
namespace PluginEngine
{
public interface IApi
{
void showMessage(string message);
void closeApplication();
void minimizeApplication();
}
}
I just copied the IApi to the dll project and build it. It is the code of dll:
namespace plugin1
{
public class Class1
{
public void onRun(PluginEngine.IApi apiObject)
{
//PluginEngine.IApi api = (IApi)apiObject;
apiObject.showMessage("Hi there...");
}
}
}
but there is an error when I want to invoke the dll method:
Object of type 'PluginEngine.ApiClass' cannot be converted to type 'PluginEngine.IApi'
Upvotes: 0
Views: 441
Reputation: 941217
I just copied the IApi to the dll project
That's where you went wrong, you cannot copy the interface. You are doing battle with the notion of type identity in .NET. The identity of a type like IApi isn't just determined by its name, it also matters what assembly it came from. So you have two distinct IApi types, the one in the plugin doesn't match the one in the host.
You need to create another class library project that contains the types that both the host and the plugin use. Like IApi. Add a reference to this library project in both your host project and your plugin project. Now there's only one IApi.
Upvotes: 2