GetFields not working on enum type (anymore)

  • Thread starter The Last Gunslinger
  • Start date
T

The Last Gunslinger

I have a method that takes a type that relates to an enum and then gets
all the Description attributes or the name and returns them in a string
array.
This was working until I added the check to see if the field was a
IsSpecialName as I was getting a value__ field I did not want.
Now, GetFields only ever returns a zero length array.
I cannot see what I have done.

The code is below

TIA
--
JB

public static string[] GetEnumDescriptions(System.Type EnumType)
{
string[] ReturnNames = new string[0];

FieldInfo[] Fields = EnumType.GetFields(BindingFlags.CreateInstance |
BindingFlags.Public);
foreach(FieldInfo Field in Fields)
{
if(!Field.IsSpecialName)
{
string[] Temp = new string[ReturnNames.Length + 1];
ReturnNames.CopyTo(Temp, 0);
ReturnNames = Temp;
Object[] Atts =
Field.GetCustomAttributes(typeof(DescriptionAttribute), false);
DescriptionAttribute Att = Atts.Length != 0 ? Atts[0] as
DescriptionAttribute : null;
if(Att != null)
{
ReturnNames[ReturnNames.GetUpperBound(0)] = Att.Description;
}
else
{
ReturnNames[ReturnNames.GetUpperBound(0)] = Field.Name;
}
}
}
return ReturnNames;
}
 
T

The Last Gunslinger

I think there might be something wrong with the project.
I put in a line to get all the names of the enum.
When I tried to watch this value I got an out of scope error.
Stepping through, the code jumped over code for no reason, straight into
the middle of the foreach loop and then exited.

I changed the cofig to release, rebuilt the proj, changed back to debug,
rebuilt and now getnames works.

Very very strange

JB
:(
 

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