public void EmitExecuteMethodBody(EmitGenerator gen, LocalBuilder instance) { // Use MakeByRefType(), because this is an out (ref) parameter in the method signature. var paramType = _dependencyProvider.TargetType.MakeByRefType(); var paramTypes = new Type[] { typeof(InjectionContext), paramType }; var createObjectMethod = _paramField.FieldType.GetMethod("CreateObject", paramTypes); if (createObjectMethod == null) { throw new ImpossibleException(); } _paramValue = gen.DeclareLocal(_dependencyProvider.TargetType); gen.LoadThis(); gen.LoadField(_paramField); gen.LoadArgument(1); gen.LoadLocalAddress(_paramValue); gen.CallMethod(createObjectMethod); }
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 } }