问题 如何向Enum添加多个属性?


我有一个名为的SQL查找表 ClientCreditResolutionPlanActionType 我想转换成一个  在

非常基本的要求,对吗?对。

我的桌子,现在 但是,有几列,或现在,需要使用它的描述属性:

  • StatusIcon
  • 状态文本
  • 的TypeText

所以我想我能做到......

namespace System.ComponentModel
{
    class StatusIconAttribute : Attribute
    {
        public string StatusIcon;
        public StatusIconAttribute(string statusIcon) { StatusIcon = statusIcon; }
    }

    class StatusTextAttribute : Attribute
    {
        public string StatusText;
        public StatusTextAttribute(string statusText) { StatusText = statusText; }
    }

    class TypeTextAttribute : Attribute
    {
        public string TypeText;
        public TypeTextAttribute(string typeText) { TypeText = typeText; }
    }
}

... 在我的 Extensions.cs 上课...

public static class EnumExtensions
{
    public static string GetStatusIcon(this Enum value)
    {
        var type = value.GetType();

        string name = Enum.GetName(type, value);              
        if (name == null) { return null; }

        var field = type.GetField(name);
        if (field == null) { return null; }

        var attr = Attribute.GetCustomAttribute(field, typeof(StatusIconAttribute)) as StatusIconAttribute;
        if (attr == null) { return null; }

        return attr.StatusIcon;
    }

    public static string GetStatusText(this Enum value)
    {
        var type = value.GetType();

        string name = Enum.GetName(type, value);              
        if (name == null) { return null; }

        var field = type.GetField(name);
        if (field == null) { return null; }

        var attr = Attribute.GetCustomAttribute(field, typeof(StatusTextAttribute)) as StatusTextAttribute;
        if (attr == null) { return null; }

        return attr.StatusText;
    }

    public static string GetTypeText(this Enum value)
    {
        var type = value.GetType();
        string name = Enum.GetName(type, value);              

        var type = value.GetType();

        string name = Enum.GetName(type, value);              
        if (name == null) { return null; }

        var field = type.GetField(name);
        if (field == null) { return null; }

        var attr = Attribute.GetCustomAttribute(field, typeof(TypeTextAttribute)) as TypeTextAttribute;
       if (attr == null) { return null; }

        return attr.TypeText;
    }
}

...最后在我的其他项目中使用它:

namespace ClientSystemServiceLibrary.Enums
{
    [DataContract]
    public enum ClientCreditResolutionPlanActionType
    {
        [EnumMember]
        [TypeText("New resolution plan submitted.")]
        [StatusText("New Plan")]
        [StatusIcon("star.png")]
        NewPlan = 1,

        [EnumMember]
        [TypeText("Resolution plan waiting on approval.")]
        [StatusText("Under Review")]
        [StatusIcon("review.png")]
        UnderReview = 2,

        [EnumMember]
        [TypeText("Resolution plan approved.")]
        [StatusText("Approved")]
        [StatusIcon("check.png")]
        Approved = 3,

        [EnumMember]
        [TypeText("Resolution plan rejected.")]
        [StatusText("Rejected")]
        [StatusIcon("cross.png")]
        Rejected = 4,

        [EnumMember]
        [TypeText("New resolution plan comment submitted.")]
        [StatusText("New Comment")]
        [StatusIcon("message.png")]
        NewComment = 5
    }
}E

除了,我认为是错的,因为我收到这些错误消息:

由于其保护级别,“System.CompenentModel.TypeTextAttribute”无法访问

找不到类型或命名空间名称'TypeText'(您是否缺少using指令或程序集引用?)

同样......对于所有3。


8259
2018-02-24 23:41


起源

你是什​​么意思,“我认为是错的”? - Blorgbeard
@Blorgbeard - 通过错误消息添加了说明;对于那个很抱歉! - Code Maverick
为什么要转换为枚举?为什么不从桌子上读? - Brad
@Brad - 因为它是UI上的Silverlight,现在我可以绑定到该特定属性。在UI方面使生活更容易。 - Code Maverick
如何获取这些属性的值? - Jitendra Pancholi


答案:


默认情况下,所有类都是内部的。如果希望可以从其他程序集访问它们,则应指定“public”访问修饰符。喜欢这个:

public class TypeTextAttribute : Attribute
{
    public string TypeText;
    public TypeTextAttribute(string typeText) { TypeText = typeText; }
}

9
2018-02-24 23:56



正是我刚才所做的......我不能把你的标记作为答案,因为我事先得到它,但我肯定会+1 - Code Maverick
哦,到底是什么......我会删除我的并给你支票= D. - Code Maverick