我通过以下代码加载并获得了一个 dll 的实例:
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 });
}
这是IApi接口的代码:
namespace PluginEngine
{
public interface IApi
{
void showMessage(string message);
void closeApplication();
void minimizeApplication();
}
}
我只是将 IApi 复制到 dll 项目并构建它。这是dll的代码:
namespace plugin1
{
public class Class1
{
public void onRun(PluginEngine.IApi apiObject)
{
//PluginEngine.IApi api = (IApi)apiObject;
apiObject.showMessage("Hi there...");
}
}
}
但是当我想调用dll方法时出现错误:
Object of type 'PluginEngine.ApiClass' cannot be converted to type 'PluginEngine.IApi'