【问题标题】:C# Load a native dll dynamic simpleC# 加载原生 dll 动态简单
【发布时间】:2014-06-03 01:49:31
【问题描述】:

所以我不知道该怎么做...

我有一个程序“Prg.cs”和一个 dll“Test.dll”。 我试过了:

Assembly asm=Assembly.Load(@"C:\Users\Me\documents\visual studio 2013\Projects\Prg\Prg\bin\Debug\Test.dll");
Type runApp = asm.GetType();
dynamic thisApp = Activator.CreateInstance(runApp, this);

但是给了我错误:

    An unhandled exception of type 'System.IO.FileLoadException' occurred in mscorlib.dll

Additional information: Could not load file or assembly 'C:\\Users\\Me\\documents\\visual studio 2013\\Projects\\Prg\\Prg\\bin\\Debug\\Test.dll' or one of its dependencies. The given assembly name or codebase was invalid. (Exception from HRESULT: 0x80131047)

谢谢!

【问题讨论】:

标签: c# class oop dynamic dll


【解决方案1】:

你可以这样做:

class Program
{
  static void Main(string[] args)
  {
    var dll = Assembly.LoadFile(@"C:\Test.dll");//The path of your dll

    var theType = dll.GetType("dll.Test");
    var c = Activator.CreateInstance(theType);
    var method = theType.GetMethod("Output");//Your method in the class
    method.Invoke(c, new object[]{@"Hello world"});

    Console.ReadLine();
  }
}

这个链接可以帮到你stackoverflow.com/a/18362515/3383479

【讨论】:

  • 我已经更新了答案。在这一行var theType = dll.GetType("dll.Test"); 中,您必须在您的情况下使用 Class,它是您的 dll 中的 Test
猜你喜欢
  • 1970-01-01
  • 2011-09-06
  • 2018-01-24
  • 2019-05-31
  • 2010-09-30
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多