Reflection Enum Type Value__ FieldInfo.isPublic

  • Thread starter Thread starter Freddy Willockx
  • Start date Start date
F

Freddy Willockx

Hi

I'm doing some actions through reflection and generating some source code to
speed up my actions.

But, when I'm generating source code to read and write the fields of an Enum
type, it goes wrong. An internal field "Value__" is added to the field.
The problem is that this field is marked as public and not as private.

Because this field is marked as public, my code generator assumes, he can
access this property directly and generates source like
MyEnum.Value__ = ReadInteger();
if the property would be marked private I would generate
FieldInfo.SetValue(MyEnum, ReadInteger());

On private fields I use reflection, on public I try to go direct.

if could test if the fieldname equals "Value__", but I find this very lame.
Is their something else I can test up on to check wheter I could access this
field directly?

kind regards

Alexander
 
when parsing an enum with reflection you need to use the inbuilt Enum object
and not the FieldInfo object.

like this:

private void ParseEnum(Type enumType)
{
string[] enumNames = Enum.GetNames(enumType);
foreach (string enumName in enumNames)
...
}
 

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