【问题标题】:VB.net load dllVB.net加载dll
【发布时间】:2014-01-11 20:05:51
【问题描述】:

我一直在尝试使用 Reflector 加载 DLL

Imports System.Reflection

我有一个使用 /CLR 用 C++ 编写的简单 DLL 文件(这是整个文件)

using namespace System;


namespace ASSEMBLE{

public class REM{
    public:
        int VALUE(){
            return 100;
        }

    };

};

在我的 VB.net 按钮点击事件中,我有

 Dim dllPath As String = "C:\Users\richard\Documents\Visual Studio 2012\Projects\link\link\bin\Release\dlltest.dll"
        ' load the assembly 
        Dim assembly1 As System.Reflection.Assembly = Assembly.LoadFrom(dllPath)
        ' get the type 
        Dim t As Type = assembly1.GetType("ASSEMBLE.REM")
        ' create an instance and add it. 
        Dim c As Object = Activator.CreateInstance(t)


        MsgBox(t.InvokeMember("VAULE", BindingFlags.Default Or BindingFlags.InvokeMethod, Nothing, c, {}))

当事件触发时(即我加载 dll)我得到错误:

Method 'ASSEMBLE.REM.VALUE' not found

使用:

<DllImport("DLL.dll")> Public Shared Function VALUE() As Integer
        End Function

不是一个选项。我需要在运行后加载 DLL。

【问题讨论】:

    标签: vb.net dll load .net-assembly reflector


    【解决方案1】:

    您的REM 类是非托管类,因此反射看不到它的方法。使用 /CLR 编译选项不会自动强制管理所有类。它只允许您在项目中拥有托管类。

    要允许调用InvokeMember,您需要将REM 设为托管类。这可以通过在类声明中添加ref 来完成,如下所示:

    公共 ref 类 REM{
        ...
    };
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2012-10-18
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-11-30
      • 1970-01-01
      • 2017-06-26
      相关资源
      最近更新 更多