下面是測試代碼:如何實現與目標函數的參數的枚舉類型反射
Public Class Class2
Public GoodName as String
Public Function someFunction()
End Function
End Class
Public Class Class1
Public Enum ENUM_VAL1_
abc = 1
def = 2
End Enum
Public Enum ERROR_VAL1_
yhn = 1
ujm = 2
End Enum
Public Function fun1(byval _enumArg1 as ENUM_VAL1_,
byval _stringArg as String,
byref _classArg Class2) as ERROR_VAL1_
... SOMETHING HERE
return something
End Function
End Class
和測試
Dim asm As Assembly = Assembly.LoadFrom(<the source>)
If asm Is Nothing Then Return Nothing
Dim typ As Type = asm.GetType(<namespace>.<class1>)
If typ Is Nothing Then Return Nothing
Dim obj As Object = Activator.CreateInstance(typ)
If obj Is Nothing Then Return Nothing
Dim arg As Object() = New Object() {ENUM_VAL1_.abc, "qazxsw", New Class2}
Dim res As Object = typ.InvokeMember(_
"fun1", _
BindingFlags.[Default] Or BindingFlags.InvokeMethod, _
Nothing, _
obj, _
arg)
的〔Dim res As Object = typ.InvokeMember(_ "fun1", _ BindingFlags.[Default] Or BindingFlags.InvokeMethod, _ Nothing, _ obj, _ arg)
] 返回一個錯誤:function '[fullname]' not found.
然後我意識到fun1簽名與我的調用不同(即使枚舉值是整數)。我做了一些研究,並在目標程序集的屬性或函數的參數與標準類型不同時找到了有關如何實現反射的「一些」示例。但我成功地將這些樣本「概念」翻譯成我的需要。
所以,在這裏我放了一些虛擬類與虛擬枚舉類型來指出問題的骨架。
原始代碼是關於Windows防火牆/端口(win7/xp/vista),其中有許多枚舉值,包括來自firewallApi.dll和Hnetcfg.dll的類型。
我的問題是,我不能「InvokeMember」(也稱爲「setProperties方法」和「setProperties方法」)用說:
Dim args As Object() = New Object() {"SQL", 6, "1433", 1}
從哪裏組裝我的功能有以下參數類型。
Public Function PortExists(_
ByVal _ruleName As String, _
ByVal _protocol As NET_FW_IP_PROTOCOL_, _
ByVal _remotePorts As String, _
ByVal _direction As NET_FW_RULE_DIRECTION_ _
) As FW_ERROR_CODE
確定,以簡化我在我自己的枚舉類型(需要被安裝任何附加的庫)
Public Function PortExists(_
ByVal _ruleName As String, _
ByVal _protocol As FW_IP_PROTOCOL, _
ByVal _remotePorts As String, _
ByVal _direction As FW_RULE_DIRECTION _
) As FW_ERROR_CODE
不知何故參數「6」和「1」應該被轉換到FW_IP_PROTOCOL
和FW_RULE_DIRECTION
類型分別...
我不知道如何!
請解釋你正在努力達到什麼以及爲什麼它不工作。怎麼了?問題是什麼? – 2012-08-07 16:53:56