You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
60 lines
1.4 KiB
60 lines
1.4 KiB
using System.Reflection;
|
|
|
|
namespace Swapping.Business.Tech;
|
|
|
|
public class InfoAttribute : Attribute
|
|
{
|
|
private string _Led;
|
|
private string _Sound;
|
|
|
|
public InfoAttribute(string led, string sound)
|
|
{
|
|
this._Led = led;
|
|
this._Sound = sound;
|
|
}
|
|
|
|
public string GetLed()
|
|
{
|
|
return this._Led;
|
|
}
|
|
|
|
public string GetSound()
|
|
{
|
|
return _Sound;
|
|
}
|
|
}
|
|
|
|
public static class InfoExtend
|
|
{
|
|
public static string GetLed(this Enum value)
|
|
{
|
|
Type type = value.GetType();
|
|
FieldInfo? fieldInfo = type.GetField(value.ToString());
|
|
if (fieldInfo != null && fieldInfo.IsDefined(typeof(InfoAttribute), true))
|
|
{
|
|
Attribute? attribute = fieldInfo.GetCustomAttribute(typeof(InfoAttribute));
|
|
if (attribute != null)
|
|
{
|
|
return ((InfoAttribute)attribute).GetLed();
|
|
}
|
|
}
|
|
|
|
return value.ToString();
|
|
}
|
|
|
|
public static string GetSound(this Enum value)
|
|
{
|
|
Type type = value.GetType();
|
|
FieldInfo? fieldInfo = type.GetField(value.ToString());
|
|
if (fieldInfo != null && fieldInfo.IsDefined(typeof(InfoAttribute), true))
|
|
{
|
|
Attribute? attribute = fieldInfo.GetCustomAttribute(typeof(InfoAttribute));
|
|
if (attribute != null)
|
|
{
|
|
return ((InfoAttribute)attribute).GetSound();
|
|
}
|
|
}
|
|
|
|
return value.ToString();
|
|
}
|
|
} |