与Visual Basic 2013链接的显式(运行时)DLL失败

时间:2015-11-17 16:21:13

标签: vb.net dll runtime

我在Visual Studio 2013上用Visual Basic编写了一个dll(现在它的作用是无关紧要的):

Namespace TextFiles

    Public Class ConfigTextFiles
        'Library of functions to handle configuration text files holding pairs of parm-name, param-value.
        'Param-name and Param-value are separated by the Delimiter property, or "=" if not set.

        Shared strDelimiter As String
        Shared intRowsCount As Integer


        Public Shared Function getParamValue(ByVal strFileLocation As String, ByVal strFileName As String, ByVal strParamName As String) As String
            'Return the value of a specific parameter within the text file
            'Return an empty string if param-name is not found
            'Return vbNullChar if file was not found

            Dim TextLine() As String
            Dim strFullFileName As String

            If Right(strFileLocation, 1) = "\" Then
                strFullFileName = strFileLocation & strFileName
            Else
                strFullFileName = strFileLocation & "\" & strFileName
            End If

            getParamValue = ""
            If System.IO.File.Exists(strFullFileName) = True Then
                If (IsNothing(strDelimiter)) Then strDelimiter = "="
                Dim objReader As New System.IO.StreamReader(strFullFileName)

                Do While objReader.Peek() <> -1
                    TextLine = Split(objReader.ReadLine(), strDelimiter)
                    If (Trim(strParamName) = Trim(TextLine(0))) Then
                        getParamValue = Trim(TextLine(1))
                        Exit Do
                    End If
                Loop
                objReader = Nothing
            Else
                MsgBox("File Does Not Exist: " & strFullFileName, MsgBoxStyle.OkOnly, "File Open Error")
                getParamValue = vbNullChar

            End If
        End Function
    End Class
End Namespace

此DLL名为:SisConfigTextFiles.dll,存储在执行(调用)(exe)文件的文件夹中。

我想在运行时从另一个Visual Basic应用程序显式链接到它:

Public Class Initializing
    Public Const CONFIG_FILE_NAME As String = "AppConfig.ini" 

    Public Shared Function getConfigParam(ParamName As String) As String
        'Load external dll
        Dim asm As Assembly = Assembly.Load("SisConfigTextFiles")
        Dim type As Type = asm.GetType("TextFiles.ConfigTextFiles")

        ' Create an instance of a Type by calling Activator.CreateInstance
        Dim dynamicObject As Object = Activator.CreateInstance(type)

        Dim appPath As String = My.Application.Info.DirectoryPath

        getConfigParam = ""
        Dim returnValue = DirectCast(type.InvokeMember("getParamValue", _
                                               BindingFlags.InvokeMethod Or BindingFlags.Static, _
                                               Nothing, dynamicObject, {appPath, CONFIG_FILE_NAME, ParamName}), String)
        getConfigParam = returnValue
    End Function

End Class

但是,我不断收到此运行时错误:

System.ArgumentNullException:值不能为null。 参数名称:type

为什么“打字”空?我该如何理顺呢?

谢谢!

2 个答案:

答案 0 :(得分:0)

你几乎就在那里,但这条线无效:

asm.GetType("TextFiles.ConfigTextFiles")

应该是

asm.GetType("SisConfigTextFiles.TextFiles.ConfigTextFiles")

您可以在Assembly.ExportedTypes属性中查看可用的类型。

答案 1 :(得分:0)

根据Sam的建议,这里是一个完全正常工作的代码,它使用延迟(运行时)绑定调用DLL中的方法:

Imports System.Reflection

Public Class Initializing
    Public Const CONFIG_FILE_NAME As String = "AppConfig.ini"

    Public Shared Sub Config_Init()

    End Sub


    Public Shared Function getConfigParam(ParamName As String) As String
        'Load external dll
        Dim asm As Assembly = Assembly.Load("SisConfigTextFiles")
        Dim type As Type = asm.GetType("SisConfigTextFiles.TextFiles.ConfigTextFiles")


        ' Create an instance of a Type by calling Activator.CreateInstance
        Dim dynamicObject As Object = Activator.CreateInstance(type)

        Dim appPath As String = My.Application.Info.DirectoryPath

        getConfigParam = ""
        'Invoking a function "setParamValue" in the dll
        Dim returnValue = DirectCast(type.InvokeMember("getParamValue", _
                                               BindingFlags.InvokeMethod Or BindingFlags.Static Or BindingFlags.Public, _
                                               Nothing, dynamicObject, {appPath, CONFIG_FILE_NAME, ParamName}), String)
        getConfigParam = returnValue
    End Function

End Class
相关问题