Esempio n. 1
0
        /// <summary>
        ///
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <param name="scope"></param>
        /// <returns></returns>
        public static T Get <T>(DIScope scope = null)
        {
            Type type = typeof(T);
            var  sd   = GetFactory(type);

            if (sd == null)
            {
                throw new KeyNotFoundException($"No implementation registration found for type {type.FullName}, did you forget to register?");
            }
            return((T)sd.Get(scope));
        }
Esempio n. 2
0
        /// <summary>
        ///
        /// </summary>
        /// <param name="scope"></param>
        /// <param name="implementor"></param>
        /// <returns></returns>
        public static object New(DIScope scope, Type implementor)
        {
            scope = scope ?? Global;

            var f = factories.GetOrAdd(implementor, i =>
            {
                ParameterExpression sp = Expression.Parameter(typeof(DIScope));

                // get public constructor...
                // should only be one...
                var first = implementor.GetTypeInfo().DeclaredConstructors.FirstOrDefault(x => x.IsPublic);

                if (first == null)
                {
                    return(Expression.Lambda <Func <DIScope, object> >(Expression.New(implementor), sp).Compile());
                }

                List <Expression> args = new List <Expression>();
                foreach (var p in first.GetParameters())
                {
                    var ptype = p.ParameterType;
                    if (ptype == typeof(DIScope))
                    {
                        args.Add(sp);
                    }
                    else
                    {
                        var sd = GetFactory(ptype);
                        if (sd == null)
                        {
                            args.Add(Expression.Constant(null));
                        }
                        else
                        {
                            args.Add(Expression.Constant(sd.Get(scope)));
                        }
                    }
                }

                Expression call = Expression.New(first, args);
                return(Expression.Lambda <Func <DIScope, object> >(call, sp).Compile());
            });

            return(scope.RegisterDisposable(f(scope)));
        }
Esempio n. 3
0
 public Object Get(DIScope scope)
 {
     return(Factory(scope));
 }
Esempio n. 4
0
 /// <summary>
 ///
 /// </summary>
 /// <param name="parent"></param>
 /// <returns></returns>
 public static DIScope NewScope(DIScope parent = null)
 {
     return(new CoreDI.DIScope(parent ?? Global));
 }
Esempio n. 5
0
 public DIScope(DIScope parent = null)
 {
     this.Parent = parent;
     parent?.children?.Add(this);
 }