PC Review


Reply
Thread Tools Rate Thread

Set an Enum property using reflection?

 
 
=?Utf-8?B?UmFlIEFuZHJld3M=?=
Guest
Posts: n/a
 
      2nd Feb 2004
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.DoubleClic
Dim oObject As New claMyClas

claReflection.SetPropertyViaReflection(oObject
End Su

Public Class claMyClas
Private meAnchor As AnchorStyle

Public Property Anchor() As AnchorStyle
Ge
Return meAncho
End Ge
Set(ByVal eAnchor As AnchorStyles
meAnchor = eAncho
End Se
End Propert
End Clas

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

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

oPropertyInfo.SetValue(oObject, iValue, Nothing

End Su
End Class
 
Reply With Quote
 
 
 
 
Mattias Sjögren
Guest
Posts: n/a
 
      3rd Feb 2004

>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 With Quote
 
=?Utf-8?B?UmFlIEFuZHJld3M=?=
Guest
Posts: n/a
 
      3rd Feb 2004
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 With Quote
 
 
 
Reply

Thread Tools
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Can I use Reflection to see if Member is Enum? Lee Microsoft C# .NET 2 13th Mar 2006 10:55 PM
UserControl Enum Property Bold in WinForms Property window Kakaiya Microsoft Dot NET 0 15th Apr 2005 04:37 AM
C# Reflection: const string from enum; how to Stuart Baker Microsoft Dot NET Framework 2 12th Sep 2004 09:39 AM
Reflection and Enum Conversion Matt Porco Microsoft Dot NET Framework 8 16th Jul 2004 07:11 PM
Reflection - dynamically invoke enum property Jenny Fletcher Microsoft VB .NET 1 17th Feb 2004 10:24 PM


Features
 

Advertising
 

Newsgroups
 


All times are GMT +1. The time now is 02:37 PM.