using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Reflection.Emit;
using System.Reflection;

namespace FastMapper
{
    
public delegate TTarget MapMethod<TTarget, TSource>(TSource source);


    
public static class FastMapper<TTarget,TSource>
    {
        
private static MapMethod<TTarget, TSource> mapMethod;
       
        
public static MapMethod<TTarget, TSource> GetMapMethod()
        {
            
if (mapMethod == null)
            {
                mapMethod 
= CreateMapMethod(typeof(TTarget), typeof(TSource));
            }
            
return mapMethod;
        }

        
public static TTarget Map(TSource source)
        {
            
if (mapMethod == null)
            {
                mapMethod 
= CreateMapMethod(typeof(TTarget), typeof(TSource));
            }
            
return mapMethod(source);     
        }

        
private static MapMethod<TTarget, TSource> CreateMapMethod(Type targetType, Type sourceType)
        {


            DynamicMethod map 
= new DynamicMethod("Map", targetType, new Type[] { sourceType }, typeof(TTarget).Module);

            ILGenerator il 
= map.GetILGenerator();
            ConstructorInfo ci 
= targetType.GetConstructor(new Type[0]);
            il.DeclareLocal(targetType);
            il.Emit(OpCodes.Newobj, ci);
            il.Emit(OpCodes.Stloc_0);
            
foreach (var sourcePropertyInfo in sourceType.GetProperties())
            {
                var targetPropertyInfo 
= (from p in targetType.GetProperties()
                                          
where p.Name == sourcePropertyInfo.Name && p.PropertyType == sourcePropertyInfo.PropertyType
                                          select p).FirstOrDefault();

                
if (targetPropertyInfo == nullcontinue;

                il.Emit(OpCodes.Ldloc_0);
                il.Emit(OpCodes.Ldarg_0);
                il.Emit(OpCodes.Callvirt, sourcePropertyInfo.GetGetMethod());
                il.Emit(OpCodes.Callvirt, targetPropertyInfo.GetSetMethod());

            }
            il.Emit(OpCodes.Ldloc_0);
            il.Emit(OpCodes.Ret);

            
return (MapMethod<TTarget, TSource>)map.CreateDelegate(typeof(MapMethod<TTarget, TSource>));

        }
    }
}

相关文章:

  • 2022-12-23
  • 2021-10-17
  • 2022-12-23
  • 2021-09-24
  • 2022-12-23
  • 2021-06-16
  • 2022-12-23
猜你喜欢
  • 2021-08-20
  • 2022-12-23
  • 2022-12-23
  • 2022-12-23
  • 2022-12-23
  • 2021-07-10
相关资源
相似解决方案