如何向枚举添加多个属性? [英] How do I add multiple attributes to an Enum?

查看:672
本文介绍了如何向枚举添加多个属性?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个名为 ClientCreditResolutionPlanActionType 的SQL查找表,我想将其转换为中,显示标记为'c#' rel = tag> c#。

I have a SQL lookup-table called ClientCreditResolutionPlanActionType that I want to convert to an enum in c#.

非常基本的请求,对吧?是的。

Very basic request, right? Right.

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

My table, now enum, however, has several columns, or now, description properties that need to go with it:


  • StatusIcon

  • StatusText

  • TypeText

所以我想我可以做...

So I figured I could do ...

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; }
    }
}

...在我的扩展程序中。 cs 类...

... in my Extensions.cs class ...

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;
    }
}

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

... and finally in my other project use it like:

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

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

Except, what I figured was wrong, as I'm receiving these error messages:


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

'System.CompenentModel.TypeTextAttribute' is inaccessible due to its protection level


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

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

对所有3个人都相同...。

Same ... for all 3.

推荐答案

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

By default, all classes are internal. You should specify "public" access modifier, if you want them to be accessible from other assemblies. Like this:

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

这篇关于如何向枚举添加多个属性?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆