コード例 #1
0
 /// <summary>
 /// Creates an object.
 /// </summary>
 /// <param name="resolver">The resolver.</param>
 /// <param name="context">The context.</param>
 /// <returns>The created object.</returns>
 private static object CreateObject(IIoCResolver resolver, CreateObjectContext context)
 {
     object[] parameters = context.ConstructorArguments
                           .Select(type => resolver.Resolve(type))
                           .ToArray();
     return(context.Constructor.Invoke(parameters));
 }
コード例 #2
0
        /// <summary>
        /// Sets a resolve container used for resolving objects (only for adapters which cannot use the same object for registration and resolving).
        /// </summary>
        /// <param name="resolver">The resolver.</param>
        /// <param name="resolverContainer">The resolver container.</param>
        public static void SetResolveContainer(this IIoCResolver resolver, object resolverContainer)
        {
            IoCReflectionAdapterBase adapterBase = resolver as IoCReflectionAdapterBase;

            if (adapterBase == null)
            {
                throw new InvalidOperationException($"Cannot set a resolver to {resolver.GetType().FullName} as the resolver is not an adapter.");
            }

            adapterBase.Resolver = resolverContainer;
        }
コード例 #3
0
ファイル: SingleObjectFactory.cs プロジェクト: Astutia/Common
        /// <summary>
        /// Creates an object.
        /// </summary>
        /// <param name="resolver">The resolver.</param>
        /// <returns>The created object.</returns>
        public override object Create(IIoCResolver resolver)
        {
            if (this.instance == null)
            {
                lock (this.syncObject)
                {
                    if (this.instance == null)
                    {
                        this.instance = this.factory(resolver);
                    }
                }
            }

            return(this.instance);
        }
コード例 #4
0
ファイル: ObjectFactory.cs プロジェクト: Astutia/Common
 /// <summary>
 /// Creates an object.
 /// </summary>
 /// <param name="resolver">The resolver.</param>
 /// <returns>The created object.</returns>
 public virtual object Create(IIoCResolver resolver)
 {
     return(this.factory(resolver));
 }
コード例 #5
0
 /// <summary>
 /// Resolves an objects.
 /// </summary>
 /// <typeparam name="TObject">The type of the object to resolve.</typeparam>
 /// <returns>The resolved object.</returns>
 public static TObject Resolve <TObject>(this IIoCResolver resolver)
 {
     return((TObject)resolver.Resolve(typeof(TObject)));
 }
コード例 #6
0
 public static T Resolve <T>(IIoCResolver resolver, params object[] parameters) where T : class
 {
     return((T)Resolve(resolver, typeof(T), parameters));
 }
コード例 #7
0
    internal static object Resolve(IIoCResolver resolver, Type wantedType, object[] parameters)
    {
        RegisterAssembly(wantedType.Assembly);

        if (TypeResolving != null)
        {
            TypeResolving(wantedType, parameters);
        }

        var sw = Stopwatch.StartNew();

        if (InitializeShortcuts.ContainsKey(wantedType))
        {
            try
            {
                var resShortcut = InitializeShortcuts[wantedType]();

                if (TypeResolved != null)
                {
                    TypeResolved(wantedType, resShortcut, sw.ElapsedMilliseconds);
                }

                return(resShortcut);
            }
            catch (Exception ex)
            {
                if (TypeResolvingFailed != null)
                {
                    TypeResolvingFailed(wantedType, ex.Skip <TargetInvocationException>());
                }

                throw;
            }
        }

        try
        {
            var res = (resolver ?? Resolver).Resolve(wantedType, parameters);

            if (TypeResolved != null)
            {
                TypeResolved(wantedType, res, sw.ElapsedMilliseconds);
            }

            return(res);
        }
        catch (TargetInvocationException ex)
        {
            if (TypeResolvingFailed != null)
            {
                TypeResolvingFailed(wantedType, ex.Skip <TargetInvocationException>());
            }

            throw new TypeResolvingFailedException(wantedType, ex.InnerException);
        }
        catch (Exception ex)
        {
            if (TypeResolvingFailed != null)
            {
                TypeResolvingFailed(wantedType, ex);
            }

            throw new TypeResolvingFailedException(wantedType, ex);
        }
    }