|
| 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
|
|
|
| |
|
| |
| |
| 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.
|
|
|
| |
|
| |
| |
| 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)
|
|
|
| |
|
|
| |
| |
| 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
|
|
|
| |
|
| |
|
|
|
|
|
|
|
|
|
|
BootFX
Reliable and powerful .NET application framework. |
|
|
|
|
|
|