2014-01-11 52 views
1

我已经尝试使用/ CLR加载使用反射VB.net负载DLL

Imports System.Reflection 

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

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。

回答

0

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

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

 
public ref class REM{ 
    ... 
};