예제 #1
0
        /// <summary>
        /// 注册核心服务
        /// </summary>
        protected virtual void RegisterCoreService()
        {
            var bindable = new BindData(this, null, null, false);

            this.Singleton <GlobalDispatcher>((_, __) => new GlobalDispatcher((paramInfos, userParams) => GetDependencies(bindable, paramInfos, userParams))).Alias <IDispatcher>();
        }
예제 #2
0
 /// <summary>
 /// 解决类类型
 /// </summary>
 /// <param name="makeServiceBindData">请求注入操作的服务绑定数据</param>
 /// <param name="service">希望解决的服务名或者别名</param>
 /// <returns>解决结果</returns>
 private object ResloveClass(BindData makeServiceBindData, string service)
 {
     return(Make(makeServiceBindData.GetContextual(service)));
 }
예제 #3
0
 /// <summary>
 /// 解决非类类型
 /// </summary>
 /// <param name="makeServiceBindData">请求注入操作的服务绑定数据</param>
 /// <param name="service">希望构造的服务名或者别名</param>
 /// <returns>解决结果</returns>
 private object ResolveNonClassAttr(BindData makeServiceBindData, string service)
 {
     return(Make(makeServiceBindData.GetContextual(service)));
 }
예제 #4
0
        /// <summary>
        /// 获取依赖解决结果
        /// </summary>
        /// <param name="makeServiceBindData">服务绑定数据</param>
        /// <param name="paramInfo">服务实例的参数信息</param>
        /// <param name="param">输入的构造参数列表</param>
        /// <returns>服务所需参数的解决结果</returns>
        /// <exception cref="RuntimeException">生成的实例类型和需求类型不一致</exception>
        private object[] GetDependencies(BindData makeServiceBindData, IList <ParameterInfo> paramInfo, IList <object> param)
        {
            var myParam = new List <object>();

            for (var i = 0; i < paramInfo.Count; i++)
            {
                var info = paramInfo[i];
                if (param != null && i < param.Count)
                {
                    if (info.ParameterType.IsInstanceOfType(param[i]))
                    {
                        myParam.Add(param[i]);
                        continue;
                    }

                    try
                    {
                        myParam.Add(Convert.ChangeType(param[i], info.ParameterType));
                    }
                    catch (Exception ex)
                    {
                        throw new RuntimeException(
                                  string.Format("Params [{0}({1})] can not convert to [{2}] , Service [{3}]",
                                                info.Name, param[i], info.ParameterType, makeServiceBindData.Service
                                                ), ex);
                    }
                    continue;
                }

                var             needService = Type2Service(info.ParameterType);
                InjectAttribute injectAttr  = null;
                if (info.IsDefined(injectTarget, false))
                {
                    var propertyAttrs = info.GetCustomAttributes(injectTarget, false);
                    if (propertyAttrs.Length > 0)
                    {
                        injectAttr = (InjectAttribute)propertyAttrs[0];
                        if (!string.IsNullOrEmpty(injectAttr.Alias))
                        {
                            needService = injectAttr.Alias;
                        }
                    }
                }

                object instance;
                if (info.ParameterType.IsClass || info.ParameterType.IsInterface)
                {
                    instance = ResloveClass(makeServiceBindData, needService);
                }
                else
                {
                    instance = ResolveNonClass(makeServiceBindData, needService);
                }

                if (injectAttr != null && injectAttr.Required && instance == null)
                {
                    throw new RuntimeException("[" + makeServiceBindData.Service + "] Required [" + info.ParameterType + "] Service.");
                }

                if (instance != null && !info.ParameterType.IsInstanceOfType(instance))
                {
                    throw new RuntimeException("[" + makeServiceBindData.Service + "] Attr inject type must be [" + info.ParameterType + "] , But instance is [" + instance.GetType() + "] Make service is [" + needService + "].");
                }

                myParam.Add(instance);
            }

            return(myParam.ToArray());
        }
예제 #5
0
 /// <summary>
 /// 常规编译一个服务
 /// </summary>
 /// <param name="makeServiceBindData">服务绑定数据</param>
 /// <param name="makeServiceType">服务类型</param>
 /// <param name="param">构造参数</param>
 /// <returns>服务实例</returns>
 private object BuildUseConcrete(BindData makeServiceBindData, Type makeServiceType, object[] param)
 {
     return(makeServiceBindData.Concrete != null?
            makeServiceBindData.Concrete(this, param) :
                BuildMake(makeServiceBindData.Service, makeServiceType, false, param));
 }