在运行时C#从dll加载多个方法

时间:2015-12-07 21:54:15

标签: c# dll runtime .net-assembly

我正在研究"插件"系统到我的程序。我想重新分发多个dll,例如:" myAwesomePlugin1.dll"," myAwesomePlugin2.dll"。每个文件都有一个方法,例如:" doSmthAwesome(string msg)"。

现在,当我准备好所有时,我想加载这两种方法并运行它们:
来自Plugin1的第一个doSmthAwesome 来自Plugin2的第二个doSmthAwesome

不幸的是,我不知道为什么,但是当我加载这些dll时,第二个复制方法来自第一个dll。我怎么能以适当的方式做到这两种方法?

继承我的代码:

string doSmthAwesomeFrom1(string msg)
{
     string value = "";
     try
     {
          Assembly a = null;
          a = Assembly.LoadFrom(pluginDirectory + "/" + IkarosConfiguration.getValueFromKey("FIRST_PLUGIN"));
          Type classType = a.GetType("awesomePlugin.awesomePlugin");
          MethodInfo mi = classType.GetMethod("doSmthAwesome");
          object obj = Activator.CreateInstance(classType);
          value = mi.Invoke(obj, new object[] { msg});
     }
     catch
     {
     }

     return value;
}


string doSmthAwesomeFrom2(string msg)
{
     string value = "";
     try
     {
          Assembly a = null;
          a = Assembly.LoadFrom(pluginDirectory + "/" + IkarosConfiguration.getValueFromKey("SECOND_PLUGIN"));
          Type classType = a.GetType("awesomePlugin.awesomePlugin");
          MethodInfo mi = classType.GetMethod("doSmthAwesome");
          object obj = Activator.CreateInstance(classType);
          value = mi.Invoke(obj, new object[] { msg});
     }
     catch
     {
     }

     return value;
}

0 个答案:

没有答案