代码之家  ›  专栏  ›  技术社区  ›  Phil Lamb

调用运行时指定的本机代码

  •  0
  • Phil Lamb  · 技术社区  · 15 年前

    我正在开发一个应用程序,允许用户从托管和本机.dll调用外部代码。用户将能够指定在运行时调用什么库/方法/函数(它将存储在配置文件中)。

    如果我知道要在编译时调用什么dll/函数,我知道如何使用pinvoke为本机库实现这一点,但是我找不到任何关于如何在运行时实现这一点的信息。

    基本上我想做的是调用一个方法:

    int result = ExecuteNativeFunction("someLibrary.dll", "foo");
    

    让它做一些相当于:

    [DllImport("someLibrary.dll")]
    static extern int foo();
    
    ...
    
    int result = foo();
    
    1 回复  |  直到 15 年前
        1
  •  2
  •   spender    15 年前

    会吗 this 是你要找的吗?使用 System.Reflection.Emit ,可以动态编译定义新PInvoke方法的代码。看到班级了吗 DllRegServer linked file 详情。

    推荐文章