属性描述
定义为PropertyInfo的扩展方法
/// <summary>/// 获取属性描述/// </summary>/// <param name="t"></param>/// <returns></returns>public static string GetDesc(this PropertyInfo t){string des = ((DescriptionAttribute)Attribute.GetCustomAttribute(t, typeof(DescriptionAttribute)))?.Description ?? null;// 属性描述return des;}
属性相关操作
是否拥有某属性,获取某属性值,设置某属性值,是否拥有某字段,获取某字段值,设置某字段值,获取实体层描述
private static BindingFlags _bindingFlags { get; }= BindingFlags.NonPublic | BindingFlags.Instance | BindingFlags.Public | BindingFlags.Static;/// <summary>/// 是否拥有某属性/// </summary>/// <param name="obj"> 对象 </param>/// <param name="propertyName"> 属性名 </param>/// <returns> </returns>public static bool ContainsProperty(this object obj, string propertyName){return obj.GetType().GetProperty(propertyName, _bindingFlags) != null;}/// <summary>/// 获取某属性值/// </summary>/// <param name="obj"> 对象 </param>/// <param name="propertyName"> 属性名 </param>/// <returns> </returns>public static object GetPropertyValue(this object obj, string propertyName){return obj.GetType().GetProperty(propertyName, _bindingFlags).GetValue(obj);}/// <summary>/// 设置某属性值/// </summary>/// <param name="obj"> 对象 </param>/// <param name="propertyName"> 属性名 </param>/// <param name="value"> 值 </param>/// <returns> </returns>public static void SetPropertyValue(this object obj, string propertyName, object value){obj.GetType().GetProperty(propertyName, _bindingFlags).SetValue(obj, value);}/// <summary>/// 是否拥有某字段/// </summary>/// <param name="obj"> 对象 </param>/// <param name="fieldName"> 字段名 </param>/// <returns> </returns>public static bool ContainsField(this object obj, string fieldName){return obj.GetType().GetField(fieldName, _bindingFlags) != null;}/// <summary>/// 获取某字段值/// </summary>/// <param name="obj"> 对象 </param>/// <param name="fieldName"> 字段名 </param>/// <returns> </returns>public static object GetGetFieldValue(this object obj, string fieldName){return obj.GetType().GetField(fieldName, _bindingFlags).GetValue(obj);}/// <summary>/// 设置某字段值/// </summary>/// <param name="obj"> 对象 </param>/// <param name="fieldName"> 字段名 </param>/// <param name="value"> 值 </param>/// <returns> </returns>public static void SetFieldValue(this object obj, string fieldName, object value){obj.GetType().GetField(fieldName, _bindingFlags).SetValue(obj, value);}/// <summary>/// 获取某字段值/// </summary>/// <param name="obj"> 对象 </param>/// <param name="methodName"> 方法名 </param>/// <returns> </returns>public static MethodInfo GetMethod(this object obj, string methodName){return obj.GetType().GetMethod(methodName, _bindingFlags);}/// <summary>/// 获取实体层描述/// </summary>/// <param name="value"> </param>/// <param name="Name"> </param>/// <returns> </returns>public static string GetEntityDescription(this Type value, string Name){PropertyDescriptor s = TypeDescriptor.GetProperties(value)[Name];DescriptionAttribute description = s == null ? null : s.Attributes[typeof(DescriptionAttribute)] as DescriptionAttribute;return description == null ? "" : description.Description;}/// <summary>/// 获取实体层描述/// </summary>/// <param name="s"> </param>/// <returns> </returns>public static string GetEntityDescription(this PropertyDescriptor s){DescriptionAttribute description = s == null ? null : s.Attributes[typeof(DescriptionAttribute)] as DescriptionAttribute;return description == null ? "" : description.Description;}
枚举描述
定义为扩展方法
/// <summary>/// 获取枚举描述/// </summary>/// <param name="value"> 枚举值 </param>/// <returns> </returns>public static string GetDescription(this Enum value){DescriptionAttribute attribute = value.GetType().GetField(value.ToString()).GetCustomAttributes(typeof(DescriptionAttribute), false).SingleOrDefault() as DescriptionAttribute;return attribute == null ? value.ToString() : attribute.Description;}/// <summary>/// 知道枚举,传入枚举英文,获取描述/// </summary>/// <param name="value"> </param>/// <param name="Name"> </param>/// <returns> </returns>public static string GetDescription(this Type value, string Name){DescriptionAttribute attribute = value.GetField(Name).GetCustomAttributes(typeof(DescriptionAttribute), false).SingleOrDefault() as DescriptionAttribute;return attribute == null ? value.ToString() : attribute.Description;}
C# .NET Core获取类属性/属性值 是否有属性/属性值 获取实体层描述 枚举描述 枚举英文获取枚举描述[Description(“xxx“)]
如果觉得《C# .NET Core获取类属性/属性值 是否有属性/属性值 获取实体层描述 枚举描述 枚》对你有帮助,请点赞、收藏,并留下你的观点哦!