using a dynamic typeof question

  • Thread starter Thread starter WebBuilder451
  • Start date Start date
W

WebBuilder451

i'm trying to modify some code to accept an enum then use reflection to fill
a dropdown list. If i name the type directly, replace enm with the real name
of the enum it works, no suprise. Question is can i make this dynamic. I
throws an error on run that it can't find the namespace. I understand why,
but is what i'm trying to do possible?

public static void BindEnumToDropDown(ref DropDownList ddl, Enum enm)
{
FieldInfo[] myEnumFields = typeof(enm).GetFields(); // ERRORS HERE
foreach (FieldInfo myField in myEnumFields)
{
if (!myField.IsSpecialName && myField.Name.ToLower() !=
"notset")
{
int myValue = (int)myField.GetValue(0);
ddl.Items.Add(new ListItem(myField.Name,
myValue.ToString()));
}
}
}
--
(i''ll be asking a lot of these, but I find C# totally way cooler than vb
and there''s no go''n back!!!)
thanks (as always)

kes
 
thanks for responding.
if i use enm.GetType() it will not compile and show mismatched paren's in
intelesense.
your are correct that the GetNames works just fine, however, i don't get the
numeric values, hense the use of reflection.

Thanks
KES
--
(i''ll be asking a lot of these, but I find C# totally way cooler than vb
and there''s no go''n back!!!)
thanks (as always)

kes
 
WebBuilder451 said:
i'm trying to modify some code to accept an enum then use reflection to fill
a dropdown list. If i name the type directly, replace enm with the real name
of the enum it works, no suprise. Question is can i make this dynamic. I
throws an error on run that it can't find the namespace. I understand why,
but is what i'm trying to do possible?

public static void BindEnumToDropDown(ref DropDownList ddl, Enum enm)
{
FieldInfo[] myEnumFields = typeof(enm).GetFields(); // ERRORS HERE
foreach (FieldInfo myField in myEnumFields)
{
if (!myField.IsSpecialName && myField.Name.ToLower() !=
"notset")
{
int myValue = (int)myField.GetValue(0);
ddl.Items.Add(new ListItem(myField.Name,
myValue.ToString()));
}
}
}

The typeof keyword only works with a type name, not a variable. Use
enm.GetType() to get the type.
 
Could you post the code that's not compiling?  I don't understand your  
description.

I think he's trying to do typeof(enm.GetType()) ...

It needs to be just enm.GetType() - typeof() is strictly compile-time.
You can easily convert a name to a value using the Enum.Parse() method.

No need for that, even. In addition to Enum.GetNames(), there's also
Enum.GetValues() - and they both return elements sorted by value, so
you can just zip those two pairwise and get name-value pairs.
 
Thank you,
This was indeed an issue of assuming more flexible means more complicated.

KES
--
(i''ll be asking a lot of these, but I find C# totally way cooler than vb
and there''s no go''n back!!!)
thanks (as always)

kes


Göran Andersson said:
WebBuilder451 said:
i'm trying to modify some code to accept an enum then use reflection to fill
a dropdown list. If i name the type directly, replace enm with the real name
of the enum it works, no suprise. Question is can i make this dynamic. I
throws an error on run that it can't find the namespace. I understand why,
but is what i'm trying to do possible?

public static void BindEnumToDropDown(ref DropDownList ddl, Enum enm)
{
FieldInfo[] myEnumFields = typeof(enm).GetFields(); // ERRORS HERE
foreach (FieldInfo myField in myEnumFields)
{
if (!myField.IsSpecialName && myField.Name.ToLower() !=
"notset")
{
int myValue = (int)myField.GetValue(0);
ddl.Items.Add(new ListItem(myField.Name,
myValue.ToString()));
}
}
}

The typeof keyword only works with a type name, not a variable. Use
enm.GetType() to get the type.
 
Thank you for the response.
I tend to prefer the Enum.Parse() when i want the value. There is something
about relying on the order of one group for another that bothers me even when
I know they are the same.

I was a bit tired when I wrote that, I also intend to use lot more than just
the value when necessary and want an easy consistent way to it. There are
certainly other ways, but call this personal choice.

i don't get the numeric values, hense the use of reflection.
I tend to use the Enum.Parse() when i want the value. There is something
about relying on the order of one group for another that get's my parinoia
I was a bit tired


--
(i''ll be asking a lot of these, but I find C# totally way cooler than vb
and there''s no go''n back!!!)
thanks (as always)

kes
 
Thank you for the response.  
I tend to prefer the Enum.Parse() when i want the value. There is something
about relying on the order of one group for another that bothers me even when
I know they are the same.

In this particular case it's safe to do it because the contracts of
both methods guarantee that pairs match. Arguably, a better design
would be to have a third method - KeyValuePair<string, Enum>[]
GetNamesAndValues() - but we have what we have.
 
I agree. Maybe it will be included in the next next version.
--
(i''ll be asking a lot of these, but I find C# totally way cooler than vb
and there''s no go''n back!!!)
thanks (as always)

kes


Pavel Minaev said:
Thank you for the response.
I tend to prefer the Enum.Parse() when i want the value. There is something
about relying on the order of one group for another that bothers me even when
I know they are the same.

In this particular case it's safe to do it because the contracts of
both methods guarantee that pairs match. Arguably, a better design
would be to have a third method - KeyValuePair<string, Enum>[]
GetNamesAndValues() - but we have what we have.
 
Back
Top