Пример #1
0
 internal ServiceContext(Type serviceType, TargetContext targetInfo) : this(serviceType)
 {
     if (targetInfo == null)
     {
         throw new ArgumentNullException("targetInfo");
     }
     objectList.Add(new ObjectContext(targetInfo));
 }
Пример #2
0
 internal ObjectContext(TargetContext targetInfo)
 {
     if (targetInfo == null)
     {
         throw new ArgumentNullException("targetInfo");
     }
     this.TargetInfo = targetInfo;
     this.Mode       = CreateMode.None;
 }
Пример #3
0
        public ObjectContext Add(TargetContext targetInfo)
        {
            if (targetInfo == null)
            {
                throw new ArgumentNullException("targetInfo");
            }
            ObjectContext objectContext = null;

            lock (this.root)
            {
                objectContext = this.objectList.Find(q => q.Mode == CreateMode.None && q.TargetInfo.TargetType == targetInfo.TargetType);
                if (objectContext == null)
                {
                    objectContext = new ObjectContext(targetInfo);
                    this.objectList.Add(objectContext);
                    this.objectList.TrimExcess();
                }
            }

            return(objectContext);
        }
Пример #4
0
        private object Create(Type serviceType, object[] args, string name, object key)
        {
            object result = null;

            if (serviceType.IsGenericType && serviceType.IsGenericTypeDefinition)
            {
                return(result);
            }

            ServiceContext serviceContext = null;
            bool           isGenericType  = false;

            if (!this.serviceDic.TryGetValue(serviceType, out serviceContext) &&
                serviceType.IsGenericType)
            {
                this.serviceDic.TryGetValue(serviceType.GetGenericTypeDefinition(), out serviceContext);
                isGenericType = true;
            }

            if (serviceContext != null)
            {
                ObjectContext objectContext = null;
                if (!string.IsNullOrEmpty(name))
                {
                    objectContext = serviceContext.GetByName(name);
                }
                else if (key != null)
                {
                    objectContext = serviceContext.GetByKey(key);
                }
                else
                {
                    objectContext = serviceContext.Get();
                }

                if (objectContext != null)
                {
                    switch (objectContext.Mode)
                    {
                    case CreateMode.Instance:
                        result = this.OnCreateEvent(serviceType, objectContext, args)
                                 ?? objectContext.Instance;
                        break;

                    case CreateMode.Method:
                        result = this.OnCreateEvent(serviceType, objectContext, args)
                                 ?? objectContext.Func.Invoke(this);
                        break;

                    case CreateMode.None:
                        var targetInfo = objectContext.TargetInfo;
                        if (isGenericType)
                        {
                            targetInfo    = new TargetContext(targetInfo.TargetType.MakeGenericType(serviceType.GetGenericArguments()));
                            objectContext = new ObjectContext(targetInfo)
                            {
                                EnabledAop  = objectContext.EnabledAop,
                                AopTypeList = objectContext.AopTypeList,
                                Name        = objectContext.Name,
                                Key         = objectContext.Key
                            };
                        }
                        var ctors = targetInfo.GetCtors();
                        foreach (var ct in ctors)
                        {
                            if (ct.IsMatch(args))
                            {
                                result = this.OnCreateEvent(serviceType, objectContext, args)
                                         ?? ((args == null || args.Length == 0)
                                            ? Activator.CreateInstance(targetInfo.TargetType)
                                            : Activator.CreateInstance(targetInfo.TargetType, args));

                                break;
                            }
                        }

                        if (result == null && (args == null || args.Length == 0))
                        {
                            CtorContext ctor = null;
                            foreach (var ct in ctors)
                            {
                                ctor = ct;
                                foreach (var t in ct.ParameterTypes)
                                {
                                    if (!this.serviceDic.ContainsKey(t) && t.IsGenericType && !t.IsGenericTypeDefinition)
                                    {
                                        var tt = t.GetGenericTypeDefinition();
                                        if (!this.serviceDic.ContainsKey(tt))
                                        {
                                            ctor = null;
                                            break;
                                        }
                                    }
                                }
                                if (ctor != null)
                                {
                                    break;
                                }
                            }

                            if (ctor != null)
                            {
                                args = new object[ctor.ParameterTypes.Length];
                                for (int i = 0; i < args.Length; i++)
                                {
                                    args[i] = this.Create(ctor.ParameterTypes[i], null, null, null);
                                }
                                result = this.OnCreateEvent(serviceType, objectContext, args)
                                         ?? Activator.CreateInstance(targetInfo.TargetType, args);
                            }
                        }
                        break;
                    }
                }
            }

            return(result);
        }