【问题标题】:How do I add multiple attributes to an Enum?如何向枚举添加多个属性?
【发布时间】:2015-02-24 23:41:22
【问题描述】:

我有一个名为 ClientCreditResolutionPlanActionType 的 SQL 查找表,我想将其转换为 中的 。

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

我的表,现在是,但是,有几列,或者现在,需要与它一起使用的描述属性:

  • 状态图标
  • 状态文本
  • 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 指令或程序集引用?)

三个都一样。

【问题讨论】:

  • 你是什么意思,“我认为是错误的”?
  • @Blorgbeard - 通过错误消息添加说明;对此感到抱歉!
  • 为什么要转换为枚举?为什么不从表格中读取?
  • @Brad - 因为它是 UI 上的 Silverlight,我现在可以绑定到该特定属性。让 UI 方面的生活更轻松。
  • 如何获取这些属性的值?

标签: enum c# enum c# enums extension-methods


【解决方案1】:

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

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

【讨论】:

  • 正是我刚刚所做的......我无法将你的答案标记为答案,因为我之前已经完成了它,但我肯定会 +1
  • 哎呀……我会删除我的并给你支票=D
猜你喜欢
  • 2020-05-11
  • 1970-01-01
  • 2022-12-14
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2022-01-18
  • 1970-01-01
  • 2011-08-17
相关资源
最近更新 更多