I am trying to enumerate all of the name/value info from a struct with
Reflection. It looks like this:



public struct MyStruct
{
public const string ValueOne = "something";
public const string ValueTwo = "somethingelse";
}

In another class, I would like to enumerate it into MemberInfo[] so I
can use the parts. How to do that in C# 2.0?

Thanks.

Re: Get Const Name and Values with Reflection? by Morten

Morten
Fri Nov 30 12:44:51 PST 2007

Hi Coconet

MemberInfo won't tell you much other than what kind of members are avail=
able. You would then need to obtain a MethodInfo, PropertyInfo or Field=
Info to get the actual values. In your case this code piece should list=
the string

MyStruct f =3D new MyStruct();
Type t =3D f.GetType();
FieldInfo[] fields =3D f.GetType().GetFields();

List<string> values =3D new List<string>();
foreach (FieldInfo fi in fields)
values.Add(fi.GetValue(fi).ToString());


On Fri, 30 Nov 2007 22:03:56 +0100, coconet <coconet@community.nospam> w=
rote:

>
> I am trying to enumerate all of the name/value info from a struct with=

> Reflection. It looks like this:
>
>
>
> public struct MyStruct
> {
> public const string ValueOne =3D "something";
> public const string ValueTwo =3D "somethingelse";
> }
>
> In another class, I would like to enumerate it into MemberInfo[] so I
> can use the parts. How to do that in C# 2.0?
>
> Thanks.
>
>
>



-- =

Happy coding!
Morten Wennevik [C# MVP]

Re: Get Const Name and Values with Reflection? by coconet

coconet
Fri Nov 30 13:31:20 PST 2007


That is gold!

Thanks.