如何在C#的API中通过Dto文件返回枚举描述而不是名称?

huangapple go评论65阅读模式
英文:

How to return enum description instead of name in API via Dto file in C#?

问题

我有一个名为FeeNameEnum的枚举,其中FeeName还包括Description。所以,我正在尝试在我的API中返回FeeName的描述。
这是枚举:

public enum FeeNameEnum
{
    [Description("Account Opening Fee (project proponent, General Account holder, Retail Aggregation Account and End User Account)")]
    AccountOpeningFee,
}

目前,在API响应中我得到的是AccountOpeningFee,这是枚举名称。有没有办法可以获取枚举名称而不是描述?

这是我的Dto文件代码:

public class FeeScheduleDto
{
    public Guid Id { get; set; }
    public FeeNameEnum FeeName { get; set; }
    public string FeeNameName { get { return FeeName.ToString(); } }
}

任何帮助将不胜感激。

英文:

I have an Enum called FeeNameEnum where the FeeName also consists of Description as well. So, I am trying to return FeeName description of FeeName in my API.
Here is the enum:

public enum FeeNameEnum
{
        [Description("Account Opening Fee (project proponent, General Account holder, Retail Aggregation Account and End User Accoun")]
        AccountOpeningFee,
}

Currently, I am getting AccountOpeningFee which is the enum name in the api response. Is there any way I can get the Description instead of the enum name?

Here is my Dto file code:

public class FeeScheduleDto
{
        public Guid Id { get; set; }
        public FeeNameEnum FeeName { get; set; }
        public string FeeNameName { get { return FeeName.ToString(); } }
}

Any help would be more than appreciable.

答案1

得分: 5

你可以使用 DescriptionAttribute 来查询枚举值的属性。在下面的代码中,GetEnumDescription演示了如何使用DescriptionAttribute。它可以用于获取任何枚举值的描述。如果没有可用的描述,它将回退到枚举值本身。

代码示例:

using System;
using System.ComponentModel;
using System.Linq;

public class Program
{
    public static string GetEnumDescription(Enum value)
    {
        if (value == null) { return ""; }

        DescriptionAttribute attribute = value.GetType()
                .GetField(value.ToString())
                ?.GetCustomAttributes(typeof(DescriptionAttribute), false)
                .SingleOrDefault() as DescriptionAttribute;
        return attribute == null ? value.ToString() : attribute.Description;
    }

    public enum FeeNameEnum
    {
        [Description("Account Opening Fee description (...")]
        AccountOpeningFee,
    }

    public static void Main(String[] args)
    {
        FeeNameEnum e = FeeNameEnum.AccountOpeningFee;
        Console.WriteLine(GetEnumDescription(e));
    }
}

输出:

Account Opening Fee description (...

在您的代码中的用法:

在您的实际代码中,您可以添加GetEnumDescription(以及using System.ComponentModel;),然后将FeeNameName属性从以下方式更改为:

public string FeeNameName { get { return FeeName.ToString(); } }

更改为:

public string FeeNameName { get { return GetEnumDescription(FeeName); } }

如果遇到错误,您可以使用以下版本的GetEnumDescription,该版本逐步执行操作,因此可以帮助识别问题的根本原因:

public static string GetEnumDescription1(Enum value)
{
    if (value == null) { return ""; }

    var type = value.GetType();
    var field = type.GetField(value.ToString());
    var custAttr = field?.GetCustomAttributes(typeof(DescriptionAttribute), false);
    DescriptionAttribute attribute = custAttr?.SingleOrDefault() as DescriptionAttribute;
    return attribute == null ? value.ToString() : attribute.Description;
}

**注意:**上面的value参数预期是一个简单的有效枚举值或多个值的“或”(|)组合。

英文:

You can use DescriptionAttribute, from System.ComponentModel, to query attributes of an enum value.
In the code below GetEnumDescription demonstrates the usage of DescriptionAttribute. It can be used to get the description of any enum value. It also falls back to the enum value itself in case a description is not available.

Code example:

using System;
using System.ComponentModel;
using System.Linq;

public class Program
{
    public static string GetEnumDescription(Enum value)
    {
        if (value == null) { return ""; }

        DescriptionAttribute attribute = value.GetType()
                .GetField(value.ToString())
                ?.GetCustomAttributes(typeof(DescriptionAttribute), false)
                .SingleOrDefault() as DescriptionAttribute;
        return attribute == null ? value.ToString() : attribute.Description;
    }

    public enum FeeNameEnum
    {
        [Description("Account Opening Fee description (...)")]
        AccountOpeningFee,
    }

    public static void Main(String[] args)
    {
        FeeNameEnum e = FeeNameEnum.AccountOpeningFee;
        Console.WriteLine(GetEnumDescription(e));
    }
}

Output:

Account Opening Fee description (...)

Usage in your code:

In your actual code you can add GetEnumDescription (and using System.ComponentModel;), then change the FeeNameName property from:

public string FeeNameName { get { return FeeName.ToString(); } }

To:

public string FeeNameName { get { return GetEnumDescription(FeeName); } }

In case you encounter errors, you can use this version of GetEnumDescription which is doing it step by step and so might help to identify where the problem comes from:

public static string GetEnumDescription1(Enum value)
{
    if (value == null) { return ""; }

    var type = value.GetType();
    var field = type.GetField(value.ToString());
    var custAttr = field?.GetCustomAttributes(typeof(DescriptionAttribute), false);
    DescriptionAttribute attribute = custAttr?.SingleOrDefault() as DescriptionAttribute;
    return attribute == null ? value.ToString() : attribute.Description;
}

Note: the argument passed to the value parameter above is expected to be either a simple valid enum value, or a flag composite - "or" (|) of several values.

答案2

得分: 1

You can use the System.ComponentModel namespace to access the DescriptionAttribute class and get the description for the FeeName enum value. Here's an example:

public class FeeScheduleDto
{
    public Guid Id { get; set; }
    public FeeNameEnum FeeName { get; set; }

    public string FeeNameDescription
    {
        get
        {
            var type = typeof(FeeNameEnum);
            var member = type.GetMember(FeeName.ToString());
            var attributes = member[0].GetCustomAttributes(typeof(DescriptionAttribute), false);

            return ((DescriptionAttribute)attributes[0]).Description;
        }
    }
}
英文:

You can use the System.ComponentModel namespace to access the DescriptionAttribute class and get the description for the FeeName enum value. Here's an example:

public class FeeScheduleDto
{
    public Guid Id { get; set; }
    public FeeNameEnum FeeName { get; set; }

    public string FeeNameDescription
    {
        get
        {
            var type = typeof(FeeNameEnum);
            var member = type.GetMember(FeeName.ToString());
            var attributes = member[0].GetCustomAttributes(typeof(DescriptionAttribute), false);

            return ((DescriptionAttribute)attributes[0]).Description;
        }
    }
}

huangapple
  • 本文由 发表于 2023年2月8日 14:23:30
  • 转载请务必保留本文链接:https://go.coder-hub.com/75382032.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定