我是 C# 新手,我有疑问,
我有一个 Web 服务(webservice1),其中有一个类.. 该类有一个枚举..
public class testnum
{
public enum test
{
[Description("1,2,3")]
123,
[Description("3,4,5")]
345,
[Description("6,7,8 ")]
678,
}
}
我正在尝试为 Web 服务创建一个客户端,并希望将枚举描述绑定到下拉列表,并将枚举值绑定到它们各自的列表项......我正在尝试类似的事情
protected void ddl1_Load(object sender, EventArgs e)
{
webservice1.Service s = new webservice1.Service();
foreach( webservice1.test value in Enum.GetValues(typeof(webservice1.test)))
{
ddl1.Items.Add(new ListItem(value.GetEnumDescription(), value.ToString()));
}
}
}
public static class ENUMHelper
{
public static string GetEnumDescription(this Enum value)
{
FieldInfo fi = value.GetType().GetField(value.ToString());
DescriptionAttribute[] attributes =
(DescriptionAttribute[])fi.GetCustomAttributes(
typeof(DescriptionAttribute),
false);
if (attributes != null &&
attributes.Length > 0)
return attributes[0].Description;
else
return value.ToString();
}
}
当我这样做时,它不会将描述放入下拉列表中..而是获得值..
有人可以让我知道我哪里出错了吗?
我什至查看了.NET 将组合框数据绑定到具有描述属性的字符串枚举,但在我的情况下它不起作用..有人可以帮忙。
PS:如果我不清楚,请告诉我,我再次解释我的问题!