C#别名特性标签Alias
/*---------------------------------------------------------------- * 作 者 :姜 彦 * 项目名称 :Utility.Data * 类 名 称 :AliasAsAttribute * 命名空间 :Utility.Data * CLR 版本 :4.0.30319.42000 * 创建时间 :2019/11/8 18:28:06 * 当前版本 :1.0.0.0 * WeChatQQ :771078740 * My Email :jiangyan2008.521@gmail.com * jiangyan2008.521@qq.com * 描述说明 : * * 修改历史 : * ******************************************************************* * Copyright @ JiangYan 2019. All rights reserved. ******************************************************************* ------------------------------------------------------------------*/ using System; using System.Collections.Generic; using System.Linq; using System.Linq.Expressions; using System.Text; namespace Utility.Data { /// <summary> /// 重命名特性 /// </summary> [AttributeUsage(AttributeTargets.Property)] public class AliasAsAttribute : Attribute { #region Construction /// <summary> /// 构造函数 /// </summary> public AliasAsAttribute(string name) { _name = name; } private string _name; #endregion #region Property public string Name { get { return _name; } } #endregion Property } public static class AliasEx { /// <summary> /// 获取AliasAs特性值 /// </summary> /// <typeparam name="T">泛型对象</typeparam> /// <param name="obj">泛型单个对象</param> /// <param name="expr">Lambda表达式</param> /// <returns></returns> public static string AliasAs<T>(this T obj, Expression<Func<T, object>> expr) { var strPropertyAlias = string.Empty;//属性的别名特性 if (expr.Body is UnaryExpression) { var body = (MemberExpression)((UnaryExpression)expr.Body).Operand; var attr = body.Member.CustomAttributes.FirstOrDefault(t => t.AttributeType == typeof(AliasAsAttribute)); if (attr == null) strPropertyAlias = body.Member.Name; else strPropertyAlias = attr.ConstructorArguments.FirstOrDefault().Value.ToString(); } else if (expr.Body is MemberExpression) { var body = expr.Body as MemberExpression; var attr = body.Member.CustomAttributes.FirstOrDefault(t => t.AttributeType == typeof(AliasAsAttribute)); if (attr == null) strPropertyAlias = body.Member.Name; else strPropertyAlias = attr.ConstructorArguments.FirstOrDefault().Value.ToString(); } else if (expr.Body is ParameterExpression) { var body = expr.Body as ParameterExpression; var attr = body.Type.CustomAttributes.FirstOrDefault(t => t.AttributeType == typeof(AliasAsAttribute)); if (attr == null) strPropertyAlias = body.Type.Name; else strPropertyAlias = attr.ConstructorArguments.FirstOrDefault().Value.ToString(); } return strPropertyAlias; } /// <summary> /// 获取AliasAs特性值 /// </summary> /// <typeparam name="T">泛型对象</typeparam> /// <param name="obj">泛型对象列表</param> /// <param name="expr">Lambda表达式</param> /// <returns></returns> public static string AliasAs<T>(this IEnumerable<T> obj, Expression<Func<T, object>> expr) { var strPropertyAlias = string.Empty;//属性的别名特性 if (expr.Body is UnaryExpression) { var body = (MemberExpression)((UnaryExpression)expr.Body).Operand; var attr = body.Member.CustomAttributes.FirstOrDefault(t => t.AttributeType == typeof(AliasAsAttribute)); if (attr == null) strPropertyAlias = body.Member.Name; else strPropertyAlias = attr.ConstructorArguments.FirstOrDefault().Value.ToString(); } else if (expr.Body is MemberExpression) { var body = expr.Body as MemberExpression; var attr = body.Member.CustomAttributes.FirstOrDefault(t => t.AttributeType == typeof(AliasAsAttribute)); if (attr == null) strPropertyAlias = body.Member.Name; else strPropertyAlias = attr.ConstructorArguments.FirstOrDefault().Value.ToString(); } else if (expr.Body is ParameterExpression) { var body = expr.Body as ParameterExpression; var attr = body.Type.CustomAttributes.FirstOrDefault(t => t.AttributeType == typeof(AliasAsAttribute)); if (attr == null) strPropertyAlias = body.Type.Name; else strPropertyAlias = attr.ConstructorArguments.FirstOrDefault().Value.ToString(); } return strPropertyAlias; } } } /*---------------------------------------------------------------- * 备 注 : * * * ******************************************************************* * Copyright @ JiangYan 2019. All rights reserved. ******************************************************************* ------------------------------------------------------------------*/
您的资助是我最大的动力!
金额随意,欢迎来赏!
我写的东西能让你能懂,那是义务
毕竟占用了你生命中的宝贵的时间和注意力
要是你还能喜欢我的作品,那就是缘分了
如果,您希望更容易地发现我的新博客,不妨点击一下绿色通道的因为,我的写作热情也离不开您的肯定支持,感谢您的阅读,我是【青青子衿】!