Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Microsoft.OpenApi.Extensions Enum.GetDisplayName() doesn't work, works fine when copied over

Wanting a quick way to set a display for Enums without creating an enum class, I came across [Display(Name = "display name")] I was going to write my own method but found Microsoft.OpenApi.Extensions.EnumExtensions.GetDisplayName.

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

It doesn't work, returning the enum.ToString() value, but if I copy over the method as is into my project, it works fine. Any idea why? Running .net 5.0 enter image description here

like image 868
Baron Avatar asked Dec 10 '25 07:12

Baron


1 Answers

They check for their own Microsoft.OpenApi.Attributes.DisplayAttribute.

Either roll your own extension method, pointing to Systems.ComponentModel.DataAnnotations.DisplayAttribute, or (less preferably) apply their DisplayAttribute to your enum.

like image 90
CodeCaster Avatar answered Dec 11 '25 22:12

CodeCaster



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!