lolol a lil complicated for me what if i do something like this, Im
having a lil trouble with it, can you tell me what im doing wrong
public enum ValidAnimals
{
[Description("BEAR")] A = 0x0000, //is there a way to get the description base off of the 0x0000
[Description("DOG ")] B = 0x0001,
[Description("CAT ")] C = 0x0002,
}
public string GetName(string Value)
{Value ="0000";
FieldInfo fi = stringValue.GetType().GetField(Value);
DescriptionAttribute[] attributes = (DescriptionAttribute[])fi.GetCustomAttributes(typeof(DescriptionAttribute), false);
return (attributes.Length > 0) ? attributes[0].Description : stringValue;
}
im basically get a 4 nibble string, and I want to do a look up via the above enum, so like "0000" means Bear, Bear is the value i want to get back
Loading
David SmithPosted Oct 15, 2010, 2:00 PM
public enum ValidAnimals
{
[Description("BEAR")] A = 0x0000,
[Description("DOG ")] B = 0x0026,
[Description("CAT ")] C = 0x0002,
}
string value = commonFunctions.GetTestName((ValidAnimals )Enum.Parse(typeof(ValidAnimals ),"0026"));
public string GetTestName(Enum eventValue)
{
ValidTestNames vtn = (ValidTestNames)eventValue;
// Get the type
Type type = eventValue.GetType();
// Get fieldinfo for this type
FieldInfo fieldInfo = type.GetField(vtn.ToString());
// Get the stringvalue attributes
DescriptionAttribute[] attribs = fieldInfo.GetCustomAttributes(
typeof(DescriptionAttribute), false) as DescriptionAttribute[]; /////////// it fails right at this line
// Return the first if there was a match.
return attribs.Length > 0 ? attribs[0].Description : null;
}
David SmithPosted Oct 15, 2010, 1:32 PM
Posted Oct 15, 2010, 12:38 PM
public enum CustomerTypeOption
{
[DescriptionAttribute("Not Specified")]
NotDefined,
[DescriptionAttribute("Corporation")]
Corp,
[DescriptionAttribute("Individual or Family")]
IndividualorFamily,
[DescriptionAttribute("School or University")]
SchoolorUniversity,
[DescriptionAttribute("Charity")]
Charity
}
Dictionary
new Dictionary
FieldInfo fi;
DescriptionAttribute da;
foreach (Customer.CustomerTypeOption enumValue in
Enum.GetValues(typeof(Customer.CustomerTypeOption)))
{
fi = typeof(Customer.CustomerTypeOption).
GetField((enumValue.ToString()));
da = (DescriptionAttribute)Attribute.GetCustomAttribute(fi,
typeof(DescriptionAttribute));
if (da != null)
{
CustomerTypeList.Add(da.Description, (int)enumValue);
}
}
// Bind the customer type combo box
CustomerTypeComboBox.DisplayMember = "Key";
CustomerTypeComboBox.ValueMember = "Value";
CustomerTypeComboBox.DataSource = new BindingSource(CustomerTypeList, null);
Felipe RamosPosted Oct 15, 2010, 11:29 AM