【问题标题】:Microsoft.OpenApi.Extensions Enum.GetDisplayName() doesn't work, works fine when copied overMicrosoft.OpenApi.Extensions Enum.GetDisplayName() 不起作用,复制过来时工作正常
【发布时间】:2022-01-03 18:45:16
【问题描述】:

想要一种在不创建枚举类的情况下为枚举设置显示的快速方法,我遇到了[Display(Name = "display name")] 本来打算自己写方法的,结果发现Microsoft.OpenApi.Extensions.EnumExtensions.GetDisplayName

public static string GetDisplayName(this Enum enumValue)
{
    var attribute = enumValue.GetAttributeOfType<DisplayAttribute>();
    return attribute == null ? enumValue.ToString() : attribute.Name;
}

它不起作用,返回 enum.ToString() 值,但如果我将方法原样复制到我的项目中,它就可以正常工作。知道为什么吗?运行.net 5.0

【问题讨论】:

    标签: c# .net enums extension-methods


    【解决方案1】:

    他们检查自己的Microsoft.OpenApi.Attributes.DisplayAttribute

    要么滚动你自己的扩展方法,指向Systems.ComponentModel.DataAnnotations.DisplayAttribute,要么(不太好)将它们的DisplayAttribute 应用到你的枚举中。

    【讨论】:

    • 完美,谢谢!当我复制它时,我错过了扩展上的使用Microsoft.OpenApi.Attributes 与自动导入的System.ComponentModel.DataAnnotations。我看到 OpenApi 是内部的,所以不能使用。
    • 我看不出他们自己推出的原因。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多