TransExpV2<TIn, TOut> { Func<TIn, TOut> cache = GetFunc(); private static Func<TIn, TOut> GetFunc() { ParameterExpression parameterExpression = Expression.Parameter(); List<MemberBinding> memberBindingList = new List<MemberBinding>(); foreach (var item in typeof(TOut).GetProperties()) { 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(); } public static TOut Trans(TIn tIn) { return cache(tIn); } }
µ÷ÓãºStudentSecond ss= TransExpV2<Student, StudentSecond>.Trans(s);
µ÷ÓÃÒ»°ÙÍò´ÎºÄʱ£º107ºÁÃë
ºÄʱԶԶµÄСÓÚʹÓÃautomapperµÄ338ºÁÃë¡£
4¡¢×ܽá
´ÓÒÔÉϵIJâÊԺͷÖÎö¿ÉÒÔºÜÈÝÒ׵óö£¬Óñí´ïʽÊ÷ÊÇ¿ÉÒԴﵽЧÂÊÓëÊéд·½Ê½¶þÕ߼汸µÄ·½·¨Ö®Ò»£¬×ÜÖ®±È´«Í³µÄÐòÁл¯ºÍ·´Éä¸ü¼ÓÓÅÐã¡£
×îºóÍû¶Ô¸÷λÓÐËù°ïÖú£¬±¾ÎÄÔ´´£¬»¶ÓÅÄשºÍÍƼö¡£
¡¡