Added UI Toolkit Progress
This commit is contained in:
55
Src/Core/Extensions/DisplayAttributeExtensions.cs
Normal file
55
Src/Core/Extensions/DisplayAttributeExtensions.cs
Normal file
@@ -0,0 +1,55 @@
|
||||
using System;
|
||||
using System.ComponentModel;
|
||||
using System.ComponentModel.DataAnnotations;
|
||||
using System.Net;
|
||||
using System.Reflection;
|
||||
|
||||
namespace BITKit
|
||||
{
|
||||
public static class DisplayAttributeExtensions
|
||||
{
|
||||
public static string GetDisplayName(this PropertyInfo self)
|
||||
{
|
||||
if (self.GetCustomAttribute<DisplayNameAttribute>() is not { } displayNameAttribute) return self.Name;
|
||||
var name = displayNameAttribute.DisplayName;
|
||||
return string.IsNullOrEmpty(name) ? self.Name : name;
|
||||
}
|
||||
public static string GetDisplayName(this FieldInfo self)
|
||||
{
|
||||
if (self.GetCustomAttribute<DisplayNameAttribute>() is not { } displayNameAttribute) return self.Name;
|
||||
var name = displayNameAttribute.DisplayName;
|
||||
return string.IsNullOrEmpty(name) ? self.Name : name;
|
||||
}
|
||||
public static string GetDisplayName(this object self)
|
||||
{
|
||||
{
|
||||
if (self.GetType().GetCustomAttribute<DisplayNameAttribute>() is { } displayNameAttribute)
|
||||
{
|
||||
return displayNameAttribute.DisplayName;
|
||||
}
|
||||
|
||||
if (self.GetType().GetCustomAttribute<DisplayAttribute>() is { } displayAttribute)
|
||||
{
|
||||
return displayAttribute.Name;
|
||||
}
|
||||
}
|
||||
{
|
||||
if (self is Enum)
|
||||
{
|
||||
var field = self.GetType().GetField(self.ToString()!);
|
||||
|
||||
if (field.GetCustomAttribute<DisplayNameAttribute>() is DisplayNameAttribute displayNameAttribute)
|
||||
{
|
||||
return displayNameAttribute.DisplayName;
|
||||
}
|
||||
|
||||
if (field.GetCustomAttribute<DisplayAttribute>() is DisplayAttribute displayAttribute)
|
||||
{
|
||||
return displayAttribute.Name;
|
||||
}
|
||||
}
|
||||
}
|
||||
return self.ToString();
|
||||
}
|
||||
}
|
||||
}
|
Reference in New Issue
Block a user