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;
}
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;
}