473,395 Members | 1,652 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,395 software developers and data experts.

Casting an Enumeration (Reflection)

Hi all,

I have a problem with an Enum and Reflection. I am using an Xml and
Reflection to create some controls, and to set their properties. All
goes well until I encounter one property which is defined like this:

public enum MyType {
Alpha = 0,
Beta = 1
}

public class MyClass {
private MyType _type;

public MyType theType {
get {
return _type;
}
set {
_type = value;
}
}
}

In another place I am using reflection to use a MyClass instance.

....
Control _control = (Control)
Assembly.LoadWithPartialName(assembly).CreateInsta nce(controlType);
Type type = _control.GetType();
PropertyInfo pi = type.GetProperty(prop.name);
pi.SetValue(_control, convertValue2PropertyType(valueToMap,
pi.PropertyType), null);

.....

the convertValue2PropertyType works fine on a lot of property types,
it does something like:

private object convertValue2PropertyType(string propValue, Type
propType)
{
if (propType.FullName == "System.Int16")
return System.Convert.ToInt16(propValue);
.....
and so on
.....
}

But when the propType.BaseType is System.Enum I cannot cast from the
propValue(which is an integer) to the proper MyType.

So if I have propValue == 0, I would like to be able to return
MyType.Alpha - Of course, in a programatic way, something like:

if (propType.BaseType.FullName == "System.Enum")
{
return (propType) System.Convert.ToInt32(propValue);
}

How do I achieve this?

TIA!
Alicia
Nov 16 '05 #1
4 8340
Alicia,

I don't see how you could return a strongly typed enumeration value
(since you are reflecting on the type, right). You could return object,
however, and then parse the value from the text ("3" to an int that equals
3). Once you have that, you can pass that value to the static ToObject
method on the Enum class, which will convert the value to the corresponding
value in the enumeration.

Hope this helps.
--
- Nicholas Paldino [.NET/C# MVP]
- mv*@spam.guard.caspershouse.com

"Alicia" <al*****@hotmail.com> wrote in message
news:d8*************************@posting.google.co m...
Hi all,

I have a problem with an Enum and Reflection. I am using an Xml and
Reflection to create some controls, and to set their properties. All
goes well until I encounter one property which is defined like this:

public enum MyType {
Alpha = 0,
Beta = 1
}

public class MyClass {
private MyType _type;

public MyType theType {
get {
return _type;
}
set {
_type = value;
}
}
}

In another place I am using reflection to use a MyClass instance.

...
Control _control = (Control)
Assembly.LoadWithPartialName(assembly).CreateInsta nce(controlType);
Type type = _control.GetType();
PropertyInfo pi = type.GetProperty(prop.name);
pi.SetValue(_control, convertValue2PropertyType(valueToMap,
pi.PropertyType), null);

....

the convertValue2PropertyType works fine on a lot of property types,
it does something like:

private object convertValue2PropertyType(string propValue, Type
propType)
{
if (propType.FullName == "System.Int16")
return System.Convert.ToInt16(propValue);
....
and so on
....
}

But when the propType.BaseType is System.Enum I cannot cast from the
propValue(which is an integer) to the proper MyType.

So if I have propValue == 0, I would like to be able to return
MyType.Alpha - Of course, in a programatic way, something like:

if (propType.BaseType.FullName == "System.Enum")
{
return (propType) System.Convert.ToInt32(propValue);
}

How do I achieve this?

TIA!
Alicia

Nov 16 '05 #2
Hi Nicholas,

Actually I try to pass 3 to the SetValue method, so the call in the end
is like:

pi.SetValue(_control, 3, null);

But the SetValue method will fail with (I can't remember the exact error
mesage) something like "type of value doesn't match the type expected on
the property". Which is true, my property expects a MyType value (even
if that is, in fact, an int), while I'm passing an int.

Thank you for your help,
Alicia

*** Sent via Developersdex http://www.developersdex.com ***
Don't just participate in USENET...get rewarded for it!
Nov 16 '05 #3
You might be looking for Enum.Parse. Good luck.

Etienne Boucher
Nov 16 '05 #4
Yup, just discovered the System.Enum.Parse method over
the weekend and it works perfect.

Thanks anyway,
Alicia
Nov 16 '05 #5

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

1
by: Sergey Poberezovskiy | last post by:
Hi, I have a simple enumeration in my schema: <xs:element name="el_1"> <xs:simpleType> <xs:restiction base="xs:string"> <xs:enumeration value="value and space 1"/> <xs:enumeration...
2
by: babylon | last post by:
I have an enum public enum MyEnum : int { X, Y } I have to do int a = (int) MyEnum.X; can i overload the operator or other means to do something like
2
by: cv | last post by:
Hi, I want to create an object from a class I load from an assembly. Then I want to cast that object to a class (an interface) 'known' to the program in order to pass it to other methods that work...
2
by: Mark | last post by:
Assume you have an enumeration like PhoneType { Home, Business, Cell }. This enumeration corresponds with a lookup/dictionary table in your database like: phone_cd | phone_descr 1 ...
3
by: Brett Kelly | last post by:
Hello all, I'm in a situation where I need to retrieve a member from the System.Data.SqlDbType enumeration knowing only the type name. At this point, I'm just trying to get reflection to...
3
by: Sampson | last post by:
I have a question about enumeration and how to populate them during runtime. I am using vb.net but will happily take any advice in c# as well. Here is an example to help illustrate what I am...
8
by: John Cobb | last post by:
Excuse me if I use some terminology incorrectly as I am not well versed in Object Orientation. In short I want to create an Enumerated type similar to the following Enum Monitor as Byte Small...
7
by: Don | last post by:
I need to get the type of an enumeration from an instance of a class. e.g. Public Class MyClass Public Enum MyEnum value1 = 1 End Enum End Class
1
by: pitdog | last post by:
This may not be the group to post this in. However I will try... I am interested in how the .NET runtime actually handles casting. For instance... This will work if you assume ChildProduct is...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.