[英]Getting enum descriptions from a class in webservice
我是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将组合框绑定到具有Description属性的字符串枚举的数据,但是在我的情况下它确实起作用..有人可以帮忙。
PS:如果我不清楚,请告诉我,我再解释一次我的问题!
解决此问题的最佳方法是将描述测试的源文件包含在客户端项目中,而不是像@tim s在他的评论之一中所建议的那样,让它生成对它的服务引用!
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.