Load/Instance Assembly/Class whose Constructor contains an enum valueparam

B

Benjamin Lukner

Hi!

I got a CE device that ships with a specific .Net dll.
Our software shall run on every device but load a special class from
that dll if it exists.
Unfortunately one of the parameters is a public enum value.
If I pass the corresponding int value I get an ArgumentException.
I was not able to cast the int value into something that's accepted.



I created a test code sample (single console module) that loads itself.
I even can load the enum into a System.Type.
If the parameter is optional I can pass Nothing w/o exception.
But I can't CType the int into the demanded enum nor pass it:

' 1. Load Assemby
' 2. Load Class1 (contains one enum and two constructors)
' 3. Step through constructors and try to instance

Imports System.Reflection
Module Module1
Sub Main()

Dim oDLL As [Assembly] = [Assembly].GetExecutingAssembly
Dim oClass As System.Type _
= oDLL.GetType(oDLL.GetName.Name + ".Class1")
Dim oType As System.Type _
= oDLL.GetType(oDLL.GetName.Name + ".Class1+Enum1")
Dim oCons() As ConstructorInfo = oClass.GetConstructors()

For Each oCon As ConstructorInfo In oCons
Dim o() As Object = New Object() {1, Nothing}
Dim oPIC As Object = oCon.Invoke(o)
oPIC = Nothing
Next

End Sub
End Module

Public Class Class1

Public Enum Enum1
Value1
Value2
End Enum

Public Sub New(ByVal Parm1 As Int32, _
Optional ByVal Parm2 As Enum1 = Enum1.Value1)
MsgBox("This works")
End Sub

Public Sub New(ByVal Parm1 As Enum1, _
Optional ByVal Parm2 As Enum1 = Enum1.Value1)
MsgBox("This ain't")
End Sub

End Class



There should be an easy solution but I couldn't find it by now.
Maybe someone can put me back on track

Regards,

Benjamin Lukner
 
N

Neil Cowburn

There should be an easy solution but I couldn't find it by now.

Parse the value using the relevant enum type and the pass it to the
constructor:

Dim i As Integer = 1
Dim enumValue As Object = [Enum].Parse(oType, i, True)
Dim params() As Object = New Object() { enumValue, Nothing }
..
..
..
oCon.Invoke(params)
 
B

Benjamin Lukner

Neil said:
There should be an easy solution but I couldn't find it by now.
Parse the value using the relevant enum type and the pass it to the
constructor:

Dim i As Integer = 1
Dim enumValue As Object = [Enum].Parse(oType, i, True)
Dim params() As Object = New Object() { enumValue, Nothing }
oCon.Invoke(params)

Thank you very much! It just works great :)

Another short question:
Is there a difference between
[Enum].Parse(oType, i, False)
and
[Enum].ToObject(oType, i)
?


And for the sake of completeness, this is a sample function call
(if someone else finds this thread and wonders how to do;
just take the sample from the original posting and extend it):

'Class1
Public Function Test(ByVal Parm1 As Enum1) As String
Return "Works!"
End Function


'Module1
Sub Main()
'[...]

Dim o() As Object = New Object() {[Enum].ToObject(oType, 1), Nothing}
Dim oPIC As Object = oCon.Invoke(o)

' Function call in separated steps:
Dim oFunc As MethodInfo = oPIC.GetType.GetMethod("Test")
Dim o2() As Object = New Object() {[Enum].ToObject(oType, 1)}
Dim oRet As Object = oFunc.Invoke(oPIC, o2)
Dim sRet As String = CType(oRet, String)
MsgBox(sRet)

' Function call in a single code line:
MsgBox _
( _
CType _
( _
oPIC.GetType.GetMethod("Test").Invoke _
( _
oPIC, _
New Object() {[Enum].ToObject(oType, 1)} _
) _
, String _
) _
)

oPIC = Nothing

'[...]
End Sub


Regards,

Benjamin Lukner
 
N

Neil Cowburn

Another short question:
Is there a difference between
[Enum].Parse(oType, i, False)
and
[Enum].ToObject(oType, i)
?

Parse works with strings; ToObject works with integral types. From a
performance perspective, you're probably better off using ToObject to
avoid the Int32->String->Enum conversion.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top