Files
raven/server/AyaNova/util/EnumAttributeExtension.cs
2018-06-28 23:41:48 +00:00

36 lines
917 B
C#

using System;
/// <summary>
/// Get custom attribute extension
/// </summary>
public static class EnumExtension
{
/// <summary>
/// Check if enum has attribute type
/// Example usage bool c = Biz.AyaType.License.HasAttribute(typeof(Biz.AttachableAttribute));
///
/// </summary>
/// <param name="value"></param>
/// <param name="t"></param>
/// <returns></returns>
public static bool HasAttribute(this Enum value, Type t)
{
var type = value.GetType();
var name = Enum.GetName(type, value);
if (name != null)
{
var field = type.GetField(name);
if (field != null)
{
var attr =
Attribute.GetCustomAttribute(field, t);
if (attr != null)
{
return true;
}
}
}
return false;
}
}