/// <summary> /// Emits the instructions that will instantiate the current implementation using the given factory functor. /// </summary> /// <param name="dependency">The dependency that describes the service to be instantiated.</param> /// <param name="serviceMap">The service map that contains the list of dependencies in the application.</param> /// <param name="targetMethod">The target method.</param> public void Emit(IDependency dependency, IDictionary <IDependency, IImplementation> serviceMap, MethodDefinition targetMethod) { var declaringType = targetMethod.DeclaringType; var module = declaringType.Module; var serviceType = module.Import(_serviceType); var createInstanceMethod = typeof(FunctorRegistry).GetMethod("CreateInstance"); var createInstance = module.Import(createInstanceMethod); // Register the functor FunctorRegistry.AddFunctor(_functorId, _functor); var body = targetMethod.Body; var IL = body.GetILProcessor(); // Instantiate the instance at runtime using the // given functor associated with the functor id IL.Emit(OpCodes.Ldstr, _functorId); IL.Emit(OpCodes.Ldarg_0); IL.Emit(OpCodes.Call, createInstance); if (serviceType.IsValueType) { return; } IL.Emit(OpCodes.Castclass, serviceType); }
internal void RegisterFunctor() { FunctorRegistry.AddFunctor(_functorId, _functor); }