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.
Morten Wennevik [C# MVP] - 30 Nov 2007 20:44 GMT
Hi Coconet
MemberInfo won't tell you much other than what kind of members are available. You would then need to obtain a MethodInfo, PropertyInfo or FieldInfo to get the actual values. In your case this code piece should list the string
MyStruct f = new MyStruct();
Type t = f.GetType();
FieldInfo[] fields = f.GetType().GetFields();
List<string> values = new List<string>();
foreach (FieldInfo fi in fields)
values.Add(fi.GetValue(fi).ToString());
> I am trying to enumerate all of the name/value info from a struct with
> Reflection. It looks like this:
[quoted text clipped - 9 lines]
>
> Thanks.

Signature
Happy coding!
Morten Wennevik [C# MVP]
coconet - 30 Nov 2007 21:31 GMT