Ejemplo n.º 1
0
        public void Convert_WithDelegate()
        {
            var type = DynamicAssemblyManager.DefineMapperType("MyClassType");
            var field = type.DefineField("Lambda", typeof(Func<int>), FieldAttributes.Static | FieldAttributes.Public);
            Func<int> getValue = () => 10;
            var source = AstBuildHelper.CallMethod(field.FieldType.GetMethod("Invoke", new Type[0]),
                                                   AstBuildHelper.ReadFieldRA(null, type.GetField("Lambda")), new List<IAstStackItem>());
            var writer = AstBuildHelper.WriteMember(typeof(DestinationObject).GetProperty("Destination"),
                                       AstBuildHelper.ReadArgumentRA(1, typeof(DestinationObject)), source);

            var convertMethod = type.DefineMethod("ConvertLambda",
                                                  MethodAttributes.Public | MethodAttributes.Static,
                                                  null, new Type[]{typeof(SourceObject), typeof(DestinationObject)});
            convertMethod.DefineParameter(1, ParameterAttributes.None, "source");
            convertMethod.DefineParameter(2, ParameterAttributes.None, "destination");
            var context = new CompilationContext(convertMethod.GetILGenerator());
            writer.Compile(context);
            new AstReturnVoid().Compile(context);
            type.CreateType();
            DynamicAssemblyManager.SaveAssembly();

            var destinationObj = new DestinationObject();
            object dynamicType = Activator.CreateInstance(type, false);
            type.GetField("Lambda").SetValue(dynamicType, getValue);
            type.InvokeMember("ConvertLambda", BindingFlags.InvokeMethod, null, dynamicType, new object[]{null, destinationObj});

            Assert.AreEqual(10, destinationObj.Destination);
        }
Ejemplo n.º 2
0
        public void Convert_Method()
        {
            var source = AstBuildHelper.ReadMemberRV(AstBuildHelper.ReadArgumentRA(0, typeof(SourceObject)),
                                                                          typeof(SourceObject).GetProperty("Source"));
            var writer = AstBuildHelper.WriteMember(typeof(DestinationObject).GetProperty("Destination"),
                                       AstBuildHelper.ReadArgumentRA(1, typeof(DestinationObject)), source);
            var type = DynamicAssemblyManager.DefineMapperType("MyClassType");
            var convertMethod = type.DefineMethod("Convert",
                                                  MethodAttributes.Public | MethodAttributes.Static,
                                                  null, new Type[]{typeof(SourceObject), typeof(DestinationObject)});
            convertMethod.DefineParameter(1, ParameterAttributes.None, "source");
            convertMethod.DefineParameter(2, ParameterAttributes.None, "destination");
            var context = new CompilationContext(convertMethod.GetILGenerator());
            writer.Compile(context);
            new AstReturnVoid().Compile(context);
            type.CreateType();
            DynamicAssemblyManager.SaveAssembly();

            var sourceObj = new SourceObject{Source = 10};
            var destinationObj = new DestinationObject();
            object dynamicType = Activator.CreateInstance(type, false);
            type.InvokeMember("Convert", BindingFlags.InvokeMethod, null, dynamicType, new object[]{sourceObj, destinationObj});

            Assert.AreEqual(sourceObj.Source, destinationObj.Destination);
        }
Ejemplo n.º 3
0
        private static Type BuildActionCallerType(string typeName, MethodInfo mi)
        {
            var  par = mi.GetParameters();
            Type actionCallerType = null;

            if (par.Length == 0)
            {
                actionCallerType = typeof(MethodInvokerAction_0);
            }
            if (par.Length == 1)
            {
                actionCallerType = typeof(MethodInvokerAction_1);
            }
            if (par.Length == 2)
            {
                actionCallerType = typeof(MethodInvokerAction_2);
            }
            if (par.Length == 3)
            {
                actionCallerType = typeof(MethodInvokerAction_3);
            }
            else
            {
                new EmitMapperException("too many method parameters");
            }

            var tb = DynamicAssemblyManager.DefineType(typeName, actionCallerType);

            MethodBuilder methodBuilder = tb.DefineMethod(
                "CallAction",
                MethodAttributes.Public | MethodAttributes.Virtual,
                null,
                Enumerable.Range(0, par.Length).Select(i => typeof(object)).ToArray()
                );

            new AstComplexNode
            {
                nodes = new List <IAstNode>
                {
                    CreateCallMethod(mi, par),
                    new AstReturnVoid()
                }
            }.Compile(new CompilationContext(methodBuilder.GetILGenerator()));

            return(tb.CreateTypeInfo());
        }
Ejemplo n.º 4
0
        /// <summary>
        /// Creates an instance of Mapper for collections.
        /// </summary>
        /// <param name="MapperName">Mapper name. It is used for registration in Mappers repositories.</param>
        /// <param name="mapperMannager">Mappers manager</param>
        /// <param name="TypeFrom">Source type</param>
        /// <param name="TypeTo">Destination type</param>
        /// <param name="SubMapper"></param>
        /// <returns></returns>
        public static MapperForCollectionImpl CreateInstance(
            string MapperName,
            ObjectMapperManager mapperMannager,
            Type TypeFrom,
            Type TypeTo,
            ObjectsMapperDescr SubMapper,
            IMappingConfigurator mappingConfigurator
            )
        {
            TypeBuilder tb = DynamicAssemblyManager.DefineType(
                "GenericListInv_" + MapperName,
                typeof(MapperForCollectionImpl)
                );

            if (TypeTo.IsGenericType() && TypeTo.GetGenericTypeDefinition() == typeof(List <>))
            {
                MethodBuilder methodBuilder = tb.DefineMethod(
                    "CopyToListInvoke",
                    MethodAttributes.Family | MethodAttributes.Virtual,
                    typeof(object),
                    new Type[] { typeof(IEnumerable) }
                    );

                InvokeCopyImpl(TypeTo, "CopyToList").Compile(new CompilationContext(methodBuilder.GetILGenerator()));

                methodBuilder = tb.DefineMethod(
                    "CopyToListScalarInvoke",
                    MethodAttributes.Family | MethodAttributes.Virtual,
                    typeof(object),
                    new Type[] { typeof(object) }
                    );

                InvokeCopyImpl(TypeTo, "CopyToListScalar").Compile(
                    new CompilationContext(methodBuilder.GetILGenerator())
                    );
            }

            MapperForCollectionImpl result = (MapperForCollectionImpl)Activator.CreateInstance(tb.CreateTypeInfo().AsType());

            result.Initialize(mapperMannager, TypeFrom, TypeTo, mappingConfigurator, null);
            result.subMapper = SubMapper;

            return(result);
        }
Ejemplo n.º 5
0
        private static Type BuildFuncCallerType(string typeName, MethodInfo mi)
        {
            var  par            = mi.GetParameters();
            Type funcCallerType = null;

            if (par.Length == 0)
            {
                funcCallerType = typeof(MethodInvokerFunc_0);
            }
            if (par.Length == 1)
            {
                funcCallerType = typeof(MethodInvokerFunc_1);
            }
            if (par.Length == 2)
            {
                funcCallerType = typeof(MethodInvokerFunc_2);
            }
            if (par.Length == 3)
            {
                funcCallerType = typeof(MethodInvokerFunc_3);
            }
            else
            {
                new EmitMapperException("too many method parameters");
            }

            var tb = DynamicAssemblyManager.DefineType(typeName, funcCallerType);

            MethodBuilder methodBuilder = tb.DefineMethod(
                "CallFunc",
                MethodAttributes.Public | MethodAttributes.Virtual,
                typeof(object),
                Enumerable.Range(0, par.Length).Select(i => typeof(object)).ToArray()
                );

            new AstReturn
            {
                returnType  = typeof(object),
                returnValue = CreateCallMethod(mi, par)
            }.Compile(new CompilationContext(methodBuilder.GetILGenerator()));

            return(tb.CreateTypeInfo());
        }
Ejemplo n.º 6
0
 public void Test_DynamicAssemblyManager()
 {
     try
     {
         DynamicAssemblyManager.SaveAssembly();
     }
     catch (Exception e)
     {
         string message = e.Message;
         if (message == ".net standard 2.0 不支持assemblyBuilder.Save 方法")
         {
             Assert.Pass();
         }
         else
         {
             Assert.Fail();
         }
     }
 }
Ejemplo n.º 7
0
        public void Create_IfNull()
        {
            var type = DynamicAssemblyManager.DefineMapperType("MyClassType");
            var convertMethod = type.DefineMethod("Create_IfNull", MethodAttributes.Public, null,
                new []{typeof(SourceObject), typeof(DestinationObject), typeof(IResourceMapper<object>), typeof(object)});

            convertMethod.DefineParameter(1, ParameterAttributes.None, "source");
            convertMethod.DefineParameter(2, ParameterAttributes.None, "destination");
            convertMethod.DefineParameter(3, ParameterAttributes.None, "mapper");
            convertMethod.DefineParameter(4, ParameterAttributes.None, "context");
            var context = new CompilationContext(convertMethod.GetILGenerator());

            new AstWriteArgument(1, typeof(DestinationObject), new AstIfNull(
                (IAstRef)AstBuildHelper.ReadArgumentRA(1, typeof(DestinationObject)),
                new AstNewObject(typeof(DestinationObject), new IAstStackItem[0])))
                .Compile(context);
            new AstReturnVoid().Compile(context);
            type.CreateType();
            DynamicAssemblyManager.SaveAssembly();
        }
Ejemplo n.º 8
0
 public EmitBuilder(IResourceMapper <TContext> mapper) : base(mapper)
 {
     _type        = DynamicAssemblyManager.DefineMapperType("ResourceMapper");
     _mapperField = _type.DefineField("Mapper", typeof(IResourceMapper <TContext>), FieldAttributes.Public | FieldAttributes.Static);
     _constructorValues.Add(_mapperField.Name, _mapper);
 }