using System;
using System.Security.Cryptography;
namespace ESUtility
{
/// <summary>
/// 类拓展方法
/// </summary>
public static class ClassExtendMethods
{
/// <summary>
/// 获取类指定的Attribute,如果没有找到返回null.
/// </summary>
public static TAttr GetAttribute<TAttr>(this Type type)
where TAttr : Attribute
{
var attributes = type.GetCustomAttributes(typeof(TAttr), false);
if (attributes.Length > 0)
return attributes[0] as TAttr;
return null;
}
}
}
针对类特性的拓展
using System;
using System.ComponentModel;
namespace ESUtility
{
/// <summary>
/// 枚举拓展方法
/// </summary>
public static class EnumExtendMethods
{
/// <summary>
/// 获取枚举指定的Attribute,如果没有找到返回null.
/// </summary>
public static TAttr GetAttribute<TAttr>(this Enum value)
where TAttr : Attribute
{
var tempType = value.GetType();
var attributes = tempType.GetMember(value.ToString())[0].GetCustomAttributes(typeof(TAttr), false);
if (attributes.Length > 0)
return attributes[0] as TAttr;
return null;
}
/// <summary>
/// 获取枚举的描述,由DescriptionAttribute指定
/// </summary>
public static string GetDescription(this Enum value)
{
var attr = value.GetAttribute<DescriptionAttribute>();
if (attr != null)
return attr.Description;
return null;
}
public static TEnum ToEnum<TEnum>(this string str)
where TEnum : struct
{
return ToEnum<TEnum>(str, default(TEnum));
}
public static TEnum ToEnum<TEnum>(this string str, TEnum defaultValue)
where TEnum : struct
{
try
{
return (TEnum)Enum.Parse(typeof(TEnum), str);
}
catch { }
return defaultValue;
}
}
}
枚举特性拓展
using System;
/// <summary>
/// 对应的属性页面-例子
/// </summary>
public class PropertyPanelAttribute : Attribute
{
/// <summary>
/// 属性面板名
/// </summary>
public Type UIPanelName { get; set; }
/// <summary>
/// 构造函数
/// </summary>
/// <param name="name"></param>
public PropertyPanelAttribute(Type name)
{
UIPanelName = name;
}
}
obase.GetType().GetAttribute<PropertyPanelAttribute>();

1461

被折叠的 条评论
为什么被折叠?



