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.
*******************************************************************
------------------------------------------------------------------*/

 

posted @ 2019-12-13 19:42  <--青青子衿-->  阅读(576)  评论(0编辑  收藏  举报
// /**/ // 在页脚Html代码 引入 // function btn_donateClick() { var DivPopup = document.getElementById('Div_popup'); var DivMasklayer = document.getElementById('div_masklayer'); DivMasklayer.style.display = 'block'; DivPopup.style.display = 'block'; var h = Div_popup.clientHeight; with (Div_popup.style) { marginTop = -h / 2 + 'px'; } } function MasklayerClick() { var masklayer = document.getElementById('div_masklayer'); var divImg = document.getElementById("Div_popup"); masklayer.style.display = "none"; divImg.style.display = "none"; } setTimeout( function () { document.getElementById('div_masklayer').onclick = MasklayerClick; document.getElementById('btn_donate').onclick = btn_donateClick; var a_gzw = document.getElementById("guanzhuwo"); a_gzw.href = "javascript:void(0);"; $("#guanzhuwo").attr("onclick","follow('33513f9f-ba13-e011-ac81-842b2b196315');"); }, 900);