コード例 #1
0
        public void EmitConstructorBody(TypeBuilder typeBuilder, EmitGenerator gen)
        {
            var mergerType = DefineParameterMergerField(typeBuilder);
            var mergerCtor = GetParameterMergerConstructor(mergerType);

            gen.LoadThis();
            gen.LoadArgument(1);
            gen.New(mergerCtor);
            gen.StoreField(_paramMergerField);
        }
コード例 #2
0
        public void EmitExecuteMethodBody(EmitGenerator gen, LocalBuilder instance)
        {
            if (_dependencyProviders == null || _dependencyProviders.Length == 0)
            {
                //var dummyClass = new DummyClass();
                gen.New(_injectedCtor);
                gen.StoreLocal(instance);
            }
            else
            {
                //IObjectRegistration p1;
                //IConfigurationModule p2;
                //ILifetimeScope p3;
                //_parameterMerger.Merge(context, out p1, out p2, out p3);
                //instance = new DummyClass(p1, p2, p3);

                var mergeMethod = _paramMergerField.FieldType.GetMethod("Merge");
                if (mergeMethod == null)
                {
                    throw new ImpossibleException();
                }

                #region Define Locals

                //IObjectRegistration p1;
                //IConfigurationModule p2;
                //ILifetimeScope p3;
                var ctorParamBuilders = new LocalBuilder[_dependencyProviders.Length];
                for (int i = 0; i < _dependencyProviders.Length; i++)
                {
                    ctorParamBuilders[i] = gen.DeclareLocal(_dependencyProviders[i].TargetType);
                }

                #endregion

                #region _parameterMerger.Merge(context, out p1, out p2, out p3);

                gen.LoadThis();
                gen.LoadField(_paramMergerField);
                gen.LoadArgument(1);
                for (int i = 0; i < ctorParamBuilders.Length; i++)
                {
                    gen.LoadLocalAddress(ctorParamBuilders[i]);
                }

                gen.CallMethod(mergeMethod);

                #endregion

                #region instance = new DummyClass(p1, p2, p3);

                for (int i = 0; i < ctorParamBuilders.Length; i++)
                {
                    gen.LoadLocal(ctorParamBuilders[i]);
                }

                gen.New(_injectedCtor);
                gen.StoreLocal(instance);

                #endregion
            }
        }