/// <summary>
        /// Generates the <see cref="Action{T}"/> delegate that will emit
        /// the necessary <see cref="IInvocationInfo"/> information.
        /// </summary>
        /// <param name="request">The <see cref="IFactoryRequest"/> instance that describes the requested service type.</param>
        /// <returns>A delegate that can emit the necessary <see cref="IInvocationInfo"/> context that will allow other developers to infer information about the method currently being executed.</returns>
        public object CreateInstance(IFactoryRequest request)
        {
            IServiceContainer container = request.Container;
            Action<MethodDefinition> result =
                method =>
                    {
                        MethodBody body = method.Body;

                        // Add the IInvocationInfo
                        // instance only once
                        bool localAlreadyExists = (from VariableDefinition local in body.Variables
                                                   where local.Name == "___invocationInfo___"
                                                   select local).Count() > 0;

                        if (localAlreadyExists)
                            return;

                        VariableDefinition variable = method.AddLocal<IInvocationInfo>();
                        variable.Name = "___invocationInfo___";

                        var emitInfo = (IEmitInvocationInfo) container.GetService(typeof (IEmitInvocationInfo));
                        emitInfo.Emit(method, method, variable);
                    };

            return result;
        }
Ejemplo n.º 2
0
        /// <summary>
        /// Generates the <see cref="Action{T1, T2}"/> instance that will
        /// weave the target type.
        /// </summary>
        /// <param name="request">The <see cref="IFactoryRequest"/> that describes the service request.</param>
        /// <returns>The <see cref="Action{T1, T2}"/> instance that will weave the target type.</returns>
        public object CreateInstance(IFactoryRequest request)
        {
            var container = request.Container;
            Action<string, TypeDefinition> result =
                (weaverName, type) =>
                {
                    // Get the method weaver instance that matches the weaverName
                    var methodWeaver = (IHostWeaver<TypeDefinition>)container.GetService(weaverName, typeof(IHostWeaver<TypeDefinition>));

                    // Wrap it in a type weaver
                    var typeWeaver = (ITypeWeaver)container.GetService("AutoMethodWeaver", typeof(ITypeWeaver), methodWeaver);

                    var module = type.Module;
                    if (!typeWeaver.ShouldWeave(type))
                        return;

                    // Modify the host module
                    typeWeaver.ImportReferences(module);
                    typeWeaver.AddAdditionalMembers(module);

                    // Weave the type itself
                    typeWeaver.Weave(type);
                };

            return result;
        }
        public object CreateInstance(IFactoryRequest request)
        {
            Type serviceType = request.ServiceType;
            Type typeArgument = serviceType.GetGenericArguments()[0];
            Type resultType = typeof (SampleGenericImplementation<>).MakeGenericType(typeArgument);

            return Activator.CreateInstance(resultType);
        }
Ejemplo n.º 4
0
        /// <summary>
        /// Instantiates the actual factory instance and uses it to instantiate the target service type.
        /// </summary>
        /// <param name="request">The <see cref="IFactoryRequest"/> that describes the service to be created.</param>
        /// <returns>A valid service instance.</returns>
        public object CreateInstance(IFactoryRequest request)
        {
            // Create the factory if necessary
            if (_realFactory == null)
                _realFactory = _getFactory(request);

            return _realFactory == null ? null : _realFactory.CreateInstance(request);
        }
Ejemplo n.º 5
0
        /// <summary>
        /// Creates a service instance using the given <paramref name="factoryRequest"/> and <see cref="IFactory"/> instance.
        /// </summary>
        /// <param name="factoryRequest">The <see cref="IFactoryRequest"/> instance that describes the context of the service request.</param>
        /// <param name="factory">The <see cref="IFactory"/> instance that will be used to instantiate the service type.</param>
        /// <returns>A valid service instance.</returns>
        public virtual object CreateFrom(IFactoryRequest factoryRequest, IFactory factory)
        {
            object instance = null;

            // Generate the service instance
            if (factory != null)
                instance = factory.CreateInstance(factoryRequest);

            return instance;
        }
Ejemplo n.º 6
0
        public object CreateInstance(IFactoryRequest request)
        {
            if (string.IsNullOrEmpty(request.ServiceName))
                throw new ArgumentNullException("ServiceName");

            var myClass = new MyClass<string>();
            myClass.Value = request.ServiceName;

            return myClass;
        }
Ejemplo n.º 7
0
        /// <summary>
        /// Generates <see cref="Action{T1, T2}"/> instances
        /// that apply a specific method weaver (with the name given in the first delegate parameter)
        /// to every type in every module of an <see cref="AssemblyDefinition"/> instance.
        /// </summary>
        /// <param name="request">The <see cref="IFactoryRequest"/> that describes the service request.</param>
        /// <returns>An action delegate that will apply a specific method weaver to all the types in the given assembly.</returns>
        public object CreateInstance(IFactoryRequest request)
        {
            var container = request.Container;
            Action<string, AssemblyDefinition> result =
                (weaverName, assembly) =>
                {
                    // Create the lambda that can modify the target types
                    var weaveWith = (Action<string, TypeDefinition>)container.GetService("TypeWeaver", typeof(Action<string, TypeDefinition>));
                    var mainModule = assembly.MainModule;

                    foreach (TypeDefinition type in mainModule.Types)
                    {
                        // Use the method weaver on the target type
                        weaveWith(weaverName, type);
                    }
                };

            return result;
        }
Ejemplo n.º 8
0
        /// <summary>
        /// Instantiates the service type using the given delegate.
        /// </summary>
        /// <param name="request">The <see cref="IFactoryRequest"/> that describes the service that needs to be created.</param>
        /// <returns>The service instance.</returns>
        public object CreateInstance(IFactoryRequest request)
        {
            object result = null;
            try
            {
                var target = _targetDelegate.Target;
                var method = _targetDelegate.Method;
                var argCount = request.Arguments.Length;
                var methodArgCount = method.GetParameters().Count();

                if (argCount != methodArgCount)
                    Console.WriteLine("Parameter Count Mismatch");
                result = _targetDelegate.DynamicInvoke(request.Arguments);
                //result = method.Invoke(target, request.Arguments);
            }
            catch (TargetInvocationException ex)
            {
                // Unroll the exception
                throw ex.InnerException;
            }

            return result;
        }
Ejemplo n.º 9
0
        /// <summary>
        /// Instantiates the service type using the given delegate.
        /// </summary>
        /// <param name="request">The <see cref="IFactoryRequest"/> that describes the service that needs to be created.</param>
        /// <returns>The service instance.</returns>
        public object CreateInstance(IFactoryRequest request)
        {
            object result = null;
            try
            {
                object target = _targetDelegate.Target;
                MethodInfo method = _targetDelegate.Method;
                int argCount = request.Arguments.Length;
                int methodArgCount = method.GetParameters().Count();

                if (argCount != methodArgCount)
                    throw new ArgumentException("Parameter Count Mismatch");

                result = _targetDelegate.DynamicInvoke(request.Arguments);
            }
            catch (TargetInvocationException ex)
            {
                // Unroll the exception
                throw ex.InnerException;
            }

            return result;
        }
Ejemplo n.º 10
0
 /// <summary>
 /// Returns the object instance that given when the <see cref="CreatorFromInstance"/> class instance was initialized.
 /// </summary>
 /// <param name="factoryRequest">The <see cref="IFactoryRequest"/> instance that describes the context of the service request.</param>
 /// <param name="factory">The <see cref="IFactory"/> instance that will be used to instantiate the service type.</param>
 /// <returns>A valid service instance.</returns>
 public object CreateFrom(IFactoryRequest factoryRequest, IFactory factory)
 {
     return _instance;
 }
 public object CreateInstance(IFactoryRequest request)
 {
     return "42";
 }
Ejemplo n.º 12
0
 public object CreateInstance(IFactoryRequest request)
 {
     return new SampleClass();
 }
Ejemplo n.º 13
0
 /// <summary>
 /// Instantiates an object reference using the given factory method.
 /// </summary>
 /// <param name="request">The <see cref="IFactoryRequest"/> instance that describes the requested service.</param>
 /// <returns>A non-null object reference that represents the service type.</returns>
 public object CreateInstance(IFactoryRequest request)
 {
     return _factoryMethod(request);
 }