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.LoadWi thPartialName(a ssembly).Create Instance(contro lType);
Type type = _control.GetTyp e();
PropertyInfo pi = type.GetPropert y(prop.name);
pi.SetValue(_co ntrol, convertValue2Pr opertyType(valu eToMap,
pi.PropertyType ), null);
.....
the convertValue2Pr opertyType works fine on a lot of property types,
it does something like:
private object convertValue2Pr opertyType(stri ng propValue, Type
propType)
{
if (propType.FullN ame == "System.Int 16")
return System.Convert. ToInt16(propVal ue);
.....
and so on
.....
}
But when the propType.BaseTy pe 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.BaseT ype.FullName == "System.Enu m")
{
return (propType) System.Convert. ToInt32(propVal ue);
}
How do I achieve this?
TIA!
Alicia 4 8360
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.co m
"Alicia" <al*****@hotmai l.com> wrote in message
news:d8******** *************** **@posting.goog le.com... 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.LoadWi thPartialName(a ssembly).Create Instance(contro lType); Type type = _control.GetTyp e(); PropertyInfo pi = type.GetPropert y(prop.name); pi.SetValue(_co ntrol, convertValue2Pr opertyType(valu eToMap, pi.PropertyType ), null);
....
the convertValue2Pr opertyType works fine on a lot of property types, it does something like:
private object convertValue2Pr opertyType(stri ng propValue, Type propType) { if (propType.FullN ame == "System.Int 16") return System.Convert. ToInt16(propVal ue); .... and so on .... }
But when the propType.BaseTy pe 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.BaseT ype.FullName == "System.Enu m") { return (propType) System.Convert. ToInt32(propVal ue); }
How do I achieve this?
TIA! Alicia
Hi Nicholas,
Actually I try to pass 3 to the SetValue method, so the call in the end
is like:
pi.SetValue(_co ntrol, 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!
You might be looking for Enum.Parse. Good luck.
Etienne Boucher
Yup, just discovered the System.Enum.Par se method over
the weekend and it works perfect.
Thanks anyway,
Alicia This thread has been closed and replies have been disabled. Please start a new discussion. Similar topics |
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 value="value2 with spaces"/>
...
|
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
|
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 with that interface.
The casting is invalid but I can't see why, as the class I load from the
assembly really is of the type I cast it to.
Command a = (Command) t.GetConstructor(new...
|
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 Home
2 Business
3 Cell
Now, I could associated each enumeration value with the phone_cd used in the
|
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 work... This is
what I've got so far:
using System;
using System.Reflection;
| |
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 after.
Create a class named “clsMyItems” and in that class place an enum.
Public Enum Items
Item1 = 0
Item2 = 1
Item3 = 2
|
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
Medium
Large
End Enum
|
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
|
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 a derived class of
BaseProduct.
BaseProduct product= new ChildProduct();
|
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it.
First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
|
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, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed.
This is as boiled down as I can make it.
Here is my compilation command:
g++-12 -std=c++20 -Wnarrowing bit_field.cpp
Here is the code in...
| |
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 Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
|
by: tracyyun |
last post by:
Dear forum friends,
With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
|
by: conductexam |
last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one.
At the time of converting from word file to html my equations which are in the word document file was convert into image.
Globals.ThisAddIn.Application.ActiveDocument.Select();...
|
by: TSSRALBI |
last post by:
Hello
I'm a network technician in training and I need your help.
I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs.
The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols.
I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
|
by: adsilva |
last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
|
by: muto222 |
last post by:
How can i add a mobile payment intergratation into php mysql website.
| |
by: bsmnconsultancy |
last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...
| |