Topaz Filer: if you use e-mail for business, we can save you money and decrease your risk.
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...
 
Auto-following on Twitter
Ubuntu and XP on one “desktop”
 
    
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...
 
Outlook interop - stopping user properties appearing on Outlook message print
Seriously, why is “cut and paste” majorly newsworthy???
 
    
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...
 
Email Archiving and Email Filing - what’s the difference?
Web-based task/todo list management
 
 
System.ArgumentException
System.EventArgs
System.Reflection.FieldInfo
System.Reflection.PropertyInfo
System.Windows.Forms.AnchorStyles




Ad
BootFX
Reliable and powerful .NET application framework.
Recession Busting Bespoke Software
Get through the recession by investing in bespoke software to decrease costs and create commercial opportunities.
Other DN247 Network Sites
.NET 247
SQL Server Wins
Old Skool Developer
 
Copyright © AMX Software Ltd 2008-2009. Portions copyright © Matthew Baxter-Reynolds 2001-2009. All rights reserved.
Contact Us - Terms of Use - Privacy Policy - .NET 247 is a member of the DN247 Network - 4.0.30129.1734