Beispiel #1
0
        private void CreateProperties(EmitTypeFactory factory, Type originalType, Type adapterType, TypeBuilder typeBldr, PropertyBuilder sourceProperty, PropertyBuilder factoryProperty)
        {
            var getSource  = ExecBld.GetProp(sourceProperty, ExecBld.GetThis());
            var getFactory = ExecBld.GetProp(factoryProperty, ExecBld.GetThis());

            var getMethod = this.GetType().GetMethod(nameof(GetValue));
            var setMethod = this.GetType().GetMethod(nameof(SetValue));

            var properties = new List <PropertyInfo>();

            int index = 0;

            foreach (var t in adapterType.GetAllInterfaceProperties())
            {
                var tProp = originalType.GetProperty(t.Name);                //, t.PropertyType);
                properties.Add(tProp);

                var get =
                    ExecBld.Return(
                        ExecBld.Cast(
                            ExecBld.Call(getMethod, getFactory, getSource, ExecBld.GetConst(index)),
                            typeof(object), tProp.PropertyType));

                var set = ExecBld.Return(
                    ExecBld.Call(setMethod, getFactory, getSource, ExecBld.GetConst(index),
                                 ExecBld.Cast(ExecBld.GetArg(1), tProp.PropertyType, typeof(object)))
                    );

                factory.AddProperty(typeBldr, t, get, set, t.DeclaringType);

                index++;
            }

            Properties = properties.ToArray();
        }
Beispiel #2
0
        private static void CreateConstructor(EmitTypeFactory factory, Type originalType, TypeBuilder typeBldr, PropertyBuilder sourceProperty, PropertyBuilder factoryProperty)
        {
            var init = ExecBld.Return(
                ExecBld.Block(
                    ExecBld.SetProp(sourceProperty, ExecBld.GetArg(0), ExecBld.GetArg(1)),
                    ExecBld.SetProp(factoryProperty, ExecBld.GetArg(0), ExecBld.GetArg(2))));

            factory.CreateConstructor(typeBldr, new[] { originalType, typeof(InternalDtoAdapterFactory) }, init);
        }
Beispiel #3
0
        private void CreateProperties(EmitTypeFactory factory, Type adapterType, TypeBuilder typeBldr)
        {
            foreach (var t in adapterType.GetAllInterfaceProperties())
            {
                var fieldName = "_" + t.Name;
                var field     = typeBldr.DefineField(fieldName, t.PropertyType, FieldAttributes.Private);

                var get = ExecBld.Return(ExecBld.GetFld(field, ExecBld.GetThis()));
                var set = ExecBld.Return(ExecBld.SetFld(field, ExecBld.GetThis(), ExecBld.GetArg(1)));

                factory.AddProperty(typeBldr, t, get, set, t.DeclaringType);
            }
        }
Beispiel #4
0
        public PureDtoFactory(EmitTypeFactory factory, Type dtoType)
        {
            var unique = Guid.NewGuid();

            var typeBldr = factory.CreateClass(
                $"generatedPureDto_{dtoType.Name}_{unique}",
                null, dtoType);

            CreateProperties(factory, dtoType, typeBldr);
            CreateConstructor(factory, typeBldr);

            m_constructor = typeBldr.CreateTypeInfo().GetConstructor(new Type[0]);
        }
Beispiel #5
0
        private void CreateProperties(EmitTypeFactory factory, Type originalType, Type adapterType, TypeBuilder typeBldr, PropertyBuilder sourceProperty)
        {
            var getSource = ExecBld.GetProp(sourceProperty, ExecBld.GetThis());

            foreach (var t in adapterType.GetAllInterfaceProperties())
            {
                var tProp = originalType.GetProperty(t.Name);                //, t.PropertyType);

                var get = ExecBld.Return(ExecBld.GetProp(tProp, getSource));
                var set = ExecBld.Return(ExecBld.SetProp(tProp, getSource, ExecBld.GetArg(1)));

                factory.AddProperty(typeBldr, t, get, set, t.DeclaringType);
            }
        }
Beispiel #6
0
        public PublicDtoAdapterFactory(EmitTypeFactory factory, Type originalType, Type adapterType)
        {
            var unique = Guid.NewGuid();

            var typeBldr = factory.CreateClass(
                $"generatedPublicAdapter_{originalType.Name}_{adapterType.Name}_{unique}",
                null, adapterType, typeof(IDtoAdapter));

            var sourcePropertyName = $"Source_{unique}";
            var sourceProperty     = factory.AddProperty(typeBldr, sourcePropertyName, originalType);

            CreateProperties(factory, originalType, adapterType, typeBldr, sourceProperty);
            CreateConstructor(factory, originalType, typeBldr, sourceProperty);
            InternalDtoAdapterFactory.CreateIDtoAdapter(factory, typeBldr, sourceProperty);

            m_constructor = typeBldr.CreateTypeInfo().GetConstructor(new[] { originalType });
        }
Beispiel #7
0
        public static void CreateIDtoAdapter(EmitTypeFactory factory, TypeBuilder typeBldr, PropertyBuilder sourceProperty)
        {
            var body = ExecBld.Return(ExecBld.GetProp(sourceProperty, ExecBld.GetThis()));

            factory.CreateMethod(typeBldr, nameof(IDtoAdapter.GetSource), typeof(object), new Type[0], typeof(IDtoAdapter), body);
        }
Beispiel #8
0
        private static void CreateConstructor(EmitTypeFactory factory, TypeBuilder typeBldr)
        {
            var init = ExecBld.Return();

            factory.CreateConstructor(typeBldr, new Type[0], init);
        }
Beispiel #9
0
        private static void CreateConstructor(EmitTypeFactory factory, Type originalType, TypeBuilder typeBldr, PropertyBuilder sourceProperty)
        {
            var init = ExecBld.Return(ExecBld.SetProp(sourceProperty, ExecBld.GetArg(0), ExecBld.GetArg(1)));

            factory.CreateConstructor(typeBldr, new[] { originalType }, init);
        }