Get Const Name and Values with Reflection?

  • Thread starter Morten Wennevik [C# MVP]
  • Start date
M

Morten Wennevik [C# MVP]

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 listthe 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:



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.
 
C

coconet

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.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top