Klaus,
How do you use your code?
It appears that you implemented the System.Enum.Parse method using
reflection.
http://msdn.microsoft.com/library/d...cpref/html/frlrfSystemEnumClassParseTopic.asp
Is there an advantage to your code over using System.Enum.Parse or the other
static/shared members of System.Enum?
Don't get me wrong, reflection is very powerful, and I like learning new
ways to use it. I'm just not seeing what specifically you are doing, or how
using reflection is "Better" then using one of the static/shared members
already available to us on System.Enum.
Thanks for any further insite!
Jay
Klaus H. Probst said:
Here's a also a way to match a literal value to an Enum member and see
if
it
exists there.
private object EnumValueFromLiteral(System.Type t, string name)
{
MemberInfo[] mi = t.FindMembers(MemberTypes.Field,
BindingFlags.Static |
BindingFlags.Public,
Type.FilterNameIgnoreCase, name);
// Can't find the thing in the enum.
if (mi.Length == 0)
throw new Exception(string.Format("Element '{0}' not found in enum
'{1}'", name, t.Name));
FieldInfo f = mi[0] as FieldInfo;
return f.GetValue(null);
}
Weird =)
--
____________________
Klaus H. Probst, MVP
http://www.vbbox.com/
mcdonamw said:
This may sound like a stupid stupid question and I figure it would be
more "general" than pertaining to a specific Language.
I'm using vb.net and I have a bunch of Const values in my program. I
can use them obviously by placing their names in place of values, hence
the ideal behind using Const.
My question is... is there a way to get the actual Variable name for a
specific value that is returned to me?
Say for instance I have:
Const varA = 20
Const varB = 30
..
Somewhere in my program, I get a value returned to me from a function.
(i.e. this is a keyboard hook I'm implementing). The result gives me
an integer value, for where I'd like to spit out the NAME of the
variable that would be associated with it.
Mind you I feel this would cause too much overhead for the programming
language to constantly compare returned values to see if they are equal
to any const values etc.
Is there anyway to do this at all? I want to stay away from say like a
major case statement because I have almost 200 Const values, for
certain Hex numbers representing each key that is pressed.
Please help if you can!
mcdonamw
------------------------------------------------------------------------------------------------------------------------------------------------