[lnkForumImage]
TotalShareware - Download Free Software

Confronta i prezzi di migliaia di prodotti.
Asp Forum
 Home | Login | Register | Search 


 

Forums >

microsoft.public.dotnet.framework.sdk

Reflection: how to get numeric values from an enum?

Ted Miller

10/10/2003 12:54:00 AM

Using reflection to parse the type info in an assembly. For enums, how to
get the constant values for the members? The FieldInfo.Value field has the
symbolic value, not the constant value. There's a HasDefault attribute but
no mention I can find about *where* the default value is actually stored.

enum Color {
Red = 1,
Green = 2,
Blue = 3
};

***

static private void ParseEnum(System.Type objType) {

FieldInfo[] arrObjFields = objType.GetFields();

foreach(FieldInfo objField in arrObjFields) {
Console.WriteLine("\t" + objField.Name + " " +
objField.GetValue(objField).ToString());
}
}

results in something like

enum Color {
Red = Red
Green = Green
Blue = Blue
};

Arg! What am I doing wrong?


4 Answers

(Mattias Sjögren)

10/10/2003 2:12:00 PM

0

Ted,

>Arg! What am I doing wrong?

The default behavior for Enum.ToString() is to return the name string,
not the value. Change it to .ToString("d") to print the integer value
instead.



Mattias

--
Mattias Sjögren [MVP] mattias @ mvps.org
http://www.msjogren.n...
Please reply only to the newsgroup.

Ted Miller

10/10/2003 10:40:00 PM

0

Well, this lloked promising, but I don't see how to use this advice. I have
a Type object for the enum, from which I have retrieved an array of
FieldInfo objects. I cannot pass "d" to any of the available ToString
methods.

I must be missing something more basic.

--

"Mattias Sjögren" <mattias.dont.want.spam@mvps.org> wrote in message
news:Ooms$izjDHA.976@tk2msftngp13.phx.gbl...
> Ted,
>
> >Arg! What am I doing wrong?
>
> The default behavior for Enum.ToString() is to return the name string,
> not the value. Change it to .ToString("d") to print the integer value
> instead.
>
>
>
> Mattias
>
> --
> Mattias Sjögren [MVP] mattias @ mvps.org
> http://www.msjogren.n...
> Please reply only to the newsgroup.


Ted Miller

10/10/2003 11:09:00 PM

0

Well, your reply spurred me to examine what I was doing, and did help. I was
overcomplicating things. The Enum type has a bunch of static methods that do
exactly what I want.

foreach(object obj in Enum.GetValues(objType)) {
Console.WriteLine("{0} =
{1}",obj.ToString(),Enum.Format(objType,obj,"d"));
}

--

"Mattias Sjögren" <mattias.dont.want.spam@mvps.org> wrote in message
news:Ooms$izjDHA.976@tk2msftngp13.phx.gbl...
> Ted,
>
> >Arg! What am I doing wrong?
>
> The default behavior for Enum.ToString() is to return the name string,
> not the value. Change it to .ToString("d") to print the integer value
> instead.
>
>
>
> Mattias
>
> --
> Mattias Sjögren [MVP] mattias @ mvps.org
> http://www.msjogren.n...
> Please reply only to the newsgroup.


Girish Bharadwaj

10/13/2003 3:37:00 PM

0

If you know that the value of the enum is going to be an int,you probably
can use Convert.ToInt32() on that GetValue. Here is what I found.
1. You need to probably create an object of the type passed in.
2. You need to pass that object you just created to the GetValue() of
FieldInfo object. (So that it can do the lookup).
3. Convert.ToInt32() on the resulting object. This should work since the
object returned through GetValue() will be an Enum and that implements
IConvertible.


HTH

--
Girish Bharadwaj
"Ted Miller" <ted@nwlink.com> wrote in message
news:voedin9m6p3c51@corp.supernews.com...
> Well, this lloked promising, but I don't see how to use this advice. I
have
> a Type object for the enum, from which I have retrieved an array of
> FieldInfo objects. I cannot pass "d" to any of the available ToString
> methods.
>
> I must be missing something more basic.
>
> --
>
> "Mattias Sjögren" <mattias.dont.want.spam@mvps.org> wrote in message
> news:Ooms$izjDHA.976@tk2msftngp13.phx.gbl...
> > Ted,
> >
> > >Arg! What am I doing wrong?
> >
> > The default behavior for Enum.ToString() is to return the name string,
> > not the value. Change it to .ToString("d") to print the integer value
> > instead.
> >
> >
> >
> > Mattias
> >
> > --
> > Mattias Sjögren [MVP] mattias @ mvps.org
> > http://www.msjogren.n...
> > Please reply only to the newsgroup.
>
>