/src/LinFu.IoC/Factories/DelegateFactory.cs
http://github.com/philiplaureano/LinFu · C# · 58 lines · 36 code · 8 blank · 14 comment · 4 complexity · a368d553114cfeb4706f636e56c25114 MD5 · raw file
- using System;
- using System.Linq;
- using System.Reflection;
- using LinFu.IoC.Interfaces;
- namespace LinFu.IoC.Factories
- {
- /// <summary>
- /// Represents a class that uses a <see cref="MulticastDelegate" />
- /// to instantiate a service instance.
- /// </summary>
- public class DelegateFactory : IFactory
- {
- private readonly MulticastDelegate _targetDelegate;
- /// <summary>
- /// Initializes the class with the given <paramref name="targetDelegate" />
- /// </summary>
- /// <param name="targetDelegate">The delegate that will be used to instantiate the factory.</param>
- public DelegateFactory(MulticastDelegate targetDelegate)
- {
- if (targetDelegate.Method.ReturnType == typeof(void))
- throw new ArgumentException("The factory delegate must have a return type.");
- _targetDelegate = targetDelegate;
- }
- /// <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)
- throw new ArgumentException("Parameter Count Mismatch");
- result = _targetDelegate.DynamicInvoke(request.Arguments);
- }
- catch (TargetInvocationException ex)
- {
- // Unroll the exception
- throw ex.InnerException;
- }
- return result;
- }
- }
- }