c# - 如何向枚举添加多个属性?

标签 c# enums extension-methods

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

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

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

  • 状态图标
  • 状态文本
  • 打字

所以我想我可以做...

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' is inaccessible due to its protection level

The type or namespace name 'TypeText' could not be found (are you missing a using directive or an assembly reference?)

所有 3 个都一样......

最佳答案

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

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

关于c# - 如何向枚举添加多个属性?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28708537/

相关文章:

c# - 每次执行 N 个线程的最佳实践是什么?

C# 在运行时从泛型类型查找方法重载

c# - 具有共享 DbContext 的简单注入(inject)器的 ASP.NET Core DI

c# - DragDrop 事件之后是否有事件发生?

ios - Swift 枚举和 CBCharacteristicProperties

c# - 如何将多个枚举传递给只接收一个的方法?

c# - 使用 lambda 获取扩展方法的第一个参数的属性? (x => x.请)

.net - 枚举扩展方法

java - 如何从枚举生成随机值并将其放入数组中?

c# - 从字典中组合 "Check Add or Fetch"