关于反射加载DLL的问题2

关于反射加载DLL的问题2
下面一段程序,大家帮忙看下
Public Overrides Function Read() As ClsRFIDTag
Dim Data(ClsRFIDTag.TagLengthBytes - 1) As Byte

If LoadDll(ClsRFIDTag.TagLengthBytes, Data) Then
Return New ClsRFIDTag(Data)
End If
Return Nothing
End Function

Private Function LoadDll(ByVal NumBytes As Integer, ByRef Data As Byte()) As Boolean
Dim assem As System.Reflection.Assembly
Dim obj As Object
Dim htype As Type
Dim meth As MethodInfo
Dim ReturnData As Boolean
Dim Parameter(1) As Object
Parameter(0) = NumBytes
Parameter(1) = Data

assem = System.Reflection.Assembly.LoadFrom(Var.RFID_DllPath)
htype = assem.GetType("ClassLibrary1.ARE") 'dll Namespace und der Name von Class
meth = htype.GetMethod("ReadBytes") ' Name der Funktion
obj = Activator.CreateInstance(htype)
ReturnData = meth.Invoke(obj, Parameter)
Return ReturnData
End Function

Dll的程序
Public Class ARE
Private Const MyPortName As String = "COM2" 'IO.Ports.SerialPort.GetPortNames(0)
Private Const MyTagBlockOffset As Integer = 20 'first block to read/write
Private MyPort As IO.Ports.SerialPort

Public Function ReadBytes(ByVal NumBytes As Integer, ByRef Data As Byte()) As Boolean
SendCommand("RST")

Try
Dim Ret As Boolean = True
For I As Integer = 0 To Fix(NumBytes / 4 + 0.99) - 1
Dim S As String = SendCommand("RD " & I + MyTagBlockOffset, True)
If S.Length < 8 Then
Ret = False
Exit For
End If
For J As Integer = 0 To 3
Data(4 * I + J) = Byte.Parse(S.Substring(2 * J, 2), Globalization.NumberStyles.HexNumber)
Next
Next
Return Ret
Catch ex As Exception
Return False
End Try
End Function

Private Function SendCommand(ByVal Cmd As String, Optional ByVal xReadData As Boolean = False) As String
MyPort.DiscardInBuffer()
MyPort.DiscardOutBuffer()
MyPort.WriteLine(Cmd)
If xReadData Then
Try
Return MyPort.ReadLine()
Catch ex As Exception
'timeout
Return ""
End Try
Else
Return ""
End If
End Function

End Class
整个程序如果不用dll动态加载的话,原来没有任何问题,现在在ReturnData = meth.Invoke(obj, Parameter)这句报错Exception has been thrown by the target of an invocation.
想问问大家可能使那里的问题??
__________________________________________________________________________
学习,Exception has been thrown by the target of an invocation 后面应该还有提示吧。。
__________________________________________________________________________
检查参数,以及是否有STATIC成员对象。
另外检查你的OBJ对象是否正确。
__________________________________________________________________________
==>ReturnData = meth.Invoke(obj, Parameter)

如果是这一句用抛出的异常,需要检查这个异常的InnerException,可能可以获得更详细的异常信息。
__________________________________________________________________________
mark
__________________________________________________________________________
InnerException 里面
system.Nullreferanzexception是对象没有实例华
innerexeeption是nothing
这说明错误是对象没有实力话吗?
__________________________________________________________________________
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值