Represents a utility class that stores factory functor instances.
コード例 #1
0
ファイル: FunctorCall.cs プロジェクト: tralivali1234/Hiro
        /// <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);
        }
コード例 #2
0
 internal void RegisterFunctor()
 {
     FunctorRegistry.AddFunctor(_functorId, _functor);
 }