比如我有一个枚举
public enum _SourceType
{
[Description("物料")]
Storage_Goods,
}我要如何获取到"物料"这个说明信息呢?如果这样不行,那么还有其它方法可以为枚举加说明信息吗?这个说明信息要在其它地方能够能过编程方式获取的到。
public enum _SourceType
{
[Description("物料")]
Storage_Goods,
}我要如何获取到"物料"这个说明信息呢?如果这样不行,那么还有其它方法可以为枚举加说明信息吗?这个说明信息要在其它地方能够能过编程方式获取的到。
Type type = typeof(Button);
Object[] attrs = type.GetCustomAttributes(typeof(DescriptionAttribute), true);
if (attrs != null && attrs.Length > 0)
{ System.ComponentModel.DescriptionAttribute desAttr = attrs[0] as System.ComponentModel.DescriptionAttribute;
MessageBox.Show(desAttr.Description);
}
DescriptionAttribute[] das = typeof(_SourceType).GetField("Storage_Goods")
.GetCustomAttributes(typeof(DescriptionAttribute), false)
.OfType<DescriptionAttribute>().ToArray();
if (das.Length > 0)
Console.WriteLine(das[0].Description);
或者:
Console.WriteLine(typeof(_SourceType).GetField("Storage_Goods")
.GetCustomAttributesData()[0].ConstructorArguments[0].Value);
{
foreach (object item in var.GetCustomAttributes(false))
{
if (item is DescriptionAttribute)
{
MessageBox.Show(var.Name+"----"+(item as DescriptionAttribute).Description);
}
}
}t是你的类