扩展方法不适用于WCF中的枚举

时间:2017-04-19 15:56:05

标签: c# 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不可用的帮助将不胜感激。感谢。

1 个答案:

答案 0 :(得分:1)

Extension方法的命名空间  (AttributesHelperExtensionNameSpace)需要添加到使用扩展名的using语句中。