zoukankan      html  css  js  c++  java
  • 对象拷贝

    转载于:https://www.cnblogs.com/lsgsanxiao/p/8205096.html(强力推荐)

    我只是代码的搬运工

    /// <summary>
        /// 
        /// </summary>
        /// <typeparam name="TIn"></typeparam>
        /// <typeparam name="TOut"></typeparam>
        public static class TransExpV2<TIn, TOut>
        {
            /// <summary>
            /// 通过表达式树复制对象
            /// </summary>
            private static readonly Func<TIn, TOut> cache = GetFunc();
            private static Func<TIn, TOut> GetFunc()
            {
                ParameterExpression parameterExpression = Expression.Parameter(typeof(TIn), "p");
                List<MemberBinding> memberBindingList = new List<MemberBinding>();
    
                foreach (var item in typeof(TOut).GetProperties())
                {
                    if (!item.CanWrite)
                        continue;
    
                    MemberExpression property = Expression.Property(parameterExpression, typeof(TIn).GetProperty(item.Name));
                    MemberBinding memberBinding = Expression.Bind(item, property);
                    memberBindingList.Add(memberBinding);
                }
    
                MemberInitExpression memberInitExpression = Expression.MemberInit(Expression.New(typeof(TOut)), memberBindingList.ToArray());
                Expression<Func<TIn, TOut>> lambda = Expression.Lambda<Func<TIn, TOut>>(memberInitExpression, new ParameterExpression[] { parameterExpression });
    
                return lambda.Compile();
            }
    
            /// <summary>
            /// 入口
            /// </summary>
            /// <param name="tIn"></param>
            /// <returns></returns>
            public static TOut Trans(TIn tIn)
            {
                return cache(tIn);
            }
  • 相关阅读:
    SQL注入攻击
    Collections使用
    jquery实现table按时间排序
    浏览器存储及使用
    android实现屏幕旋转切换fragment
    安卓保存图片到图库demo总结
    android service基础
    正则表达式学习总结
    注解和枚举回顾
    Vue+Element项目日期选择器类型转化的问题
  • 原文地址:https://www.cnblogs.com/likui-bookHouse/p/10038870.html
Copyright © 2011-2022 走看看