Abstract classes, static field values and reflection

  • Thread starter Thread starter Torben K. Jensen
  • Start date Start date
T

Torben K. Jensen

I've hit the wall with this one:

Using System.Reflection.FieldInfo, I can retrieve the values of any field from an instantiated class and constant fields from abstract classes.
However, it seems impossible to retrieve the values of static fields in an abstract class. Is there any way to do this?

My concrete goal is to retrieve a list containing all static string values in System.Windows.Forms.DataFormats, so I can make a S.W.F.Design.ComponentEditorPage containing a CheckBox for each format used in drag&drop operations.

Any hints are greatly appreciated.
 
Torben said:
My concrete goal is to retrieve a list containing all static string
values in System.Windows.Forms.DataFormats, so I can make a
S.W.F.Design.ComponentEditorPage containing a CheckBox for each format
used in drag&drop operations.

I found the solution:


FieldInfo[] info = typeof(DataFormats).GetFields();

foreach (FieldInfo field in info) {
listBox1.Items.Add(field.GetValue(null));
}


I had no idea you could pass a null value to FieldInfo.GetValue() to retrieve the values of static fields, but apparently you can. Whowouldathunk.
 
"Torben K. Jensen" <[email protected]> a écrit dans le message de (e-mail address removed)...

| I had no idea you could pass a null value to FieldInfo.GetValue() to
retrieve the values of static fields, but apparently you can.
Whowouldathunk.

Whowouldathunk ? The kind of person who reads the help ? :-))

Joanna
 

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

Back
Top