Reflection - dynamically invoke enum property

  • Thread starter Thread starter Jenny Fletcher
  • Start date Start date
J

Jenny Fletcher

I am trying to dynamically invoke a property of enumeration type but I can't
find a way to get it to work. This is my code which works fine until I call
InvokeMember and I get an ArgumentException. I've tried various combinations
of parameters in my call to InvokeMember but can't find anything that does
the job. Any help would be appreciated.

Jenny

Dim arrObj(0) As Object
Dim objPInfo As PropertyInfo = TextBox1.GetType.GetProperty("ScrollBars")
If Not objPInfo Is Nothing Then
If objPInfo.CanWrite Then
If objPInfo.PropertyType.IsEnum Then
Dim objEnumType As Type = objPInfo.PropertyType
Dim objFieldInfo As FieldInfo = objEnumType.GetField("Both")
If Not objFieldInfo Is Nothing Then
arrObj(0) = Activator.CreateInstance(objEnumType)
arrObj(0).GetType.InvokeMember(objFieldInfo.Name,
BindingFlags.SetField, Nothing, arrObj(0), Nothing)
End If
End If
End If

TextBox1.GetType.InvokeMember("ScrollBars", BindingFlags.SetProperty,
Nothing, TextBox1, arrObj)
 
Jenny,
arrObj(0).GetType.InvokeMember(objFieldInfo.Name,
BindingFlags.SetField, Nothing, arrObj(0), Nothing)

I'm not sure what you're trying to do here. If you want to set the
property value, you should try using

objPInfo.SetValue(TextBox1, Enum.Parse(objEnumType, "Both"), Nothing)



Mattias
 
Back
Top