0
我正尝试在WCF中为枚举使用扩展方法。这里是扩展方法的类:扩展方法不适用于WCF中的枚举
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Runtime.Serialization;
using System.ComponentModel;
using AttributesHelperExtensionNameSpace;
namespace Ratu
{
[DataContract]
public enum StatusCode
{
[EnumMember]
[Description("C")]
Closed = 0,
[EnumMember]
[Description("A")]
Cancelled = 1
}
}
namespace AttributesHelperExtensionNameSpace
{
public static class AttributesHelperExtension
{
public static string ToDescription(this Enum value)
{
var da = (DescriptionAttribute[])(value.GetType().GetField(value.ToString())).GetCustomAttributes(typeof(DescriptionAttribute), false);
return da.Length > 0 ? da[0].Description : value.ToString();
}
}
}
然后我试图提取描述:
StatusCode status = StatusCode.Closed;
string test = status.ToDescription(); // The Error is here
但我歌厅的错误:
Ratu.StatusCode does not contain a definition for 'ToDescription' and no extension method 'ToDescription' accepting a first argument of type 'Ratu.StatusCode' could be found (are you missing a using directive or an assembly reference?)
为何任何帮助ToDescription不可用,将不胜感激。谢谢。
在((DescriptionAttribute)da [0])处强制再次强制转换。说明 –
您确定在尝试使用扩展方法的代码中使用了AttributesHelperExtensionNameSpace;如果你尝试'AttributesHelperExtension.ToDescription(status)',会怎么样? – juharr