Multimobile Development: Building Applications for any Smartphone
Set an Enum property using reflection?
Messages   Related Types
This message was discovered on microsoft.public.dotnet.framework.


Rae Andrews
Is there any way to set the value of a property that has been declared as an Enum type using reflection?

Basically I'm trying to write a generic procedure that will parse through an XML node and set any matching properties of an object passed in to the corresponding values. This works great for any of the primitive types, but i cannot get it to work if the property is an enum. I've found that using reflection I can loop through the static fields of the Enum type and if the value of one corresponds to the value I am trying to set I can use the GetValue on the FieldInfo object to set the value, but this does not work if the value is two or more of the Enum values OR'd together.

In the example here, the PropertyInfo.SetValue call will die with an "Object type cannot be converted to target type" error. Is there any way to do a conversion on the value so that it will be accepted?

Private Sub Form1_DoubleClick(ByVal sender As Object, ByVal e As System.EventArgs) Handles MyBase.DoubleClick
Dim oObject As New claMyClass

claReflection.SetPropertyViaReflection(oObject)
End Sub

Public Class claMyClass
Private meAnchor As AnchorStyles

Public Property Anchor() As AnchorStyles
Get
Return meAnchor
End Get
Set(ByVal eAnchor As AnchorStyles)
meAnchor = eAnchor
End Set
End Property
End Class

Public Class claReflection
Public Shared Sub SetPropertyViaReflection(ByVal oObject As Object)
Dim sPropertyName As String = "Anchor"
Dim iValue As Int32 = 3

Dim oPropertyInfo As Reflection.PropertyInfo = oObject.GetType.GetProperty(sPropertyName)

oPropertyInfo.SetValue(oObject, iValue, Nothing)

End Sub
End Class
Reply to this message...
Vote that this is a GOOD answer...
 
Really good experience at the Apple Store
MonoDroid – looking *awesome*
 
    
Mattias Sjögren
>Is there any way to do a conversion on the value so that it will be accepted?

Dim oValue As Object = [Enum].Parse(oPropertyInfo.PropertyType,
iValue.ToString())
oPropertyInfo.SetValue(oObject, oValue, Nothing)

Mattias

--
Mattias Sjögren [MVP] mattias @ mvps.org
http://www.msjogren.net/dotnet/ | http://www.dotnetinterop.com
Please reply only to the newsgroup.
Reply to this message...
Vote that this is a GOOD answer...
 
First volume of Multimobile Development nearly ready to go to press
A mention on Developing for the iPhone and Android: The pros and cons
 
    
Rae Andrews
Thank Mattias,

Turns out what I was looking for was similar to your suggestion....

oPropertyInfo.SetValue(oObject, System.Enum.ToObject(oPropertyInfo.PropertyType, oValue), Nothing)
Reply to this message...
Vote that this is a GOOD answer...
 
 
    
Ed
Hi rae,
I'm developing under Visual Basic .NET for Compact Framework.
I have a similar problem as you, but for me the solution unfortunately doesn't work.

In case of finding an Enum I try the same as you proposed:

pInfo = Me.GetType.GetProperty(propertyName)

pInfo.SetValue(Me, System.Enum.ToObject(pInfo.PropertyType, value), Nothing)

But I get an ArgumentException.
I cannot imagine that the Compact Framework is the reason for that.
Could anyone please help?

Thanks in advance
Reply to this message...
Vote that this is a GOOD answer...
 
First chapters of Multimobile Development book now available on Apress Alpha program
iPad
 
 
System.ArgumentException
System.EventArgs
System.Reflection.FieldInfo
System.Reflection.PropertyInfo
System.Windows.Forms.AnchorStyles




Multimobile Development: Building Applications for any Smartphone
Ad
BootFX
Reliable and powerful .NET application framework.
iOS, Android and Windows Phone Development Training and Consultancy
Hosted by RackSRV Communications
 
Multimobile Development: Building Applications for any Smartphone
Copyright © AMX Software Ltd 2008-2010. Portions copyright © Matthew Baxter-Reynolds 2001-2010. All rights reserved.
Contact Us - Terms of Use - Privacy Policy - 4.0.30129.1734