By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
458,084 Members | 1,199 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 458,084 IT Pros & Developers. It's quick & easy.

Reflection - dynamically invoke enum property

P: n/a
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)
Nov 20 '05 #1
Share this Question
Share on Google+
1 Reply


P: n/a
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

--
Mattias Sjögren [MVP] mattias @ mvps.org
http://www.msjogren.net/dotnet/ | http://www.dotnetinterop.com
Please reply only to the newsgroup.
Nov 20 '05 #2

This discussion thread is closed

Replies have been disabled for this discussion.