/// <summary>获取成员绑定的显示名,优先DisplayName,然后Description</summary> /// <param name="member">成员对象</param> /// <param name="inherit">是否从继承对象中搜索</param> /// <returns></returns> public static string YusGetDesc(this MemberInfo member, bool inherit = true) { DescriptionAttribute customAttribute = member.YusGetCustomAttribute <DescriptionAttribute>(inherit); if (customAttribute != null && !customAttribute.Description.YusNullOrWhiteSpace()) { return(customAttribute.Description); } return((string)null); }
/// <summary>获取自定义属性的值。可用于ReflectionOnly加载的程序集</summary> /// <typeparam name="TAttribute"></typeparam> /// <typeparam name="TResult"></typeparam> /// <param name="target">目标对象</param> /// <param name="inherit">是否递归</param> /// <returns></returns> public static TResult GetCustomAttributeValue <TAttribute, TResult>(this MemberInfo target, bool inherit = true) where TAttribute : Attribute { if (target == null) { return(default(TResult)); } try { var list = CustomAttributeData.GetCustomAttributes(target); if (list != null && list.Count > 0) { foreach (var item in list) { if (typeof(TAttribute).FullName != item.Constructor.DeclaringType.FullName) { continue; } var args = item.ConstructorArguments; if (args != null && args.Count > 0) { return((TResult)args[0].Value); } } } if (inherit && target is Type) { target = (target as Type).BaseType; if (target != null && target != typeof(object)) { return(GetCustomAttributeValue <TAttribute, TResult>(target, inherit)); } } } catch { // 出错以后,如果不是仅反射加载,可以考虑正面来一次 if (!target.Module.Assembly.ReflectionOnly) { //var att = GetCustomAttribute<TAttribute>(target, inherit); var att = target.YusGetCustomAttribute <TAttribute>(inherit); if (att != null) { var pi = typeof(TAttribute).GetProperties().FirstOrDefault(p => p.PropertyType == typeof(TResult)); if (pi != null) { return((TResult)att.GetValue(pi)); } } } } return(default(TResult)); }
/// <summary>获取成员绑定的显示名,优先DisplayName,然后Description</summary> /// <param name="member"></param> /// <param name="inherit"></param> /// <returns></returns> public static string GetDescription(this MemberInfo member, bool inherit = true) { var att2 = member.YusGetCustomAttribute <DescriptionAttribute>(inherit); if (att2 != null && !att2.Description.YusNullOrWhiteSpace()) { return(att2.Description); } return(null); }
/// <summary>获取成员绑定的显示名,优先DisplayName,然后Description</summary> /// <param name="member"></param> /// <param name="inherit"></param> /// <returns></returns> public static string GetDisplayName(this MemberInfo member, bool inherit = true) { var att = member.YusGetCustomAttribute <DisplayNameAttribute>(inherit); if (att != null && !att.DisplayName.YusNullOrWhiteSpace()) { return(att.DisplayName); } return(null); }
/// <summary> /// 获取指定泛型类型的自定义属性 /// </summary> /// <typeparam name="T">要获取的属性类型</typeparam> /// <param name="element">成员信息</param> /// <param name="inherit">是否从继承对象中搜索</param> /// <returns></returns> public static T YusGetCustomAttribute <T>(this MemberInfo element, bool inherit) where T : Attribute { return((T)element.YusGetCustomAttribute(typeof(T), inherit)); }