Example #1
0
        /// <summary>
        /// 对于memberInfo 或者  parameterInfo 进行设值
        /// </summary>
        /// <param name="classType"></param>
        /// <param name="memberType"></param>
        /// <param name="memberInfo"></param>
        /// <param name="parameterInfo"></param>
        /// <returns></returns>
        private object Resolve(Type classType, Type memberType, MemberInfo memberInfo, ParameterInfo parameterInfo = null)
        {
            if (classType == null)
            {
                return(null);
            }
            if (string.IsNullOrEmpty(this.value))
            {
                return(null);
            }
            try
            {
                if (!this.value.StartsWith("#{") || !this.value.EndsWith("}"))
                {
                    var parameterValue = this.value;
                    var parseValue     = parameterInfo == null
                        ? TypeManipulation.ChangeToCompatibleType(parameterValue, memberType, memberInfo)
                        : TypeManipulation.ChangeToCompatibleType(parameterValue, memberType, parameterInfo);

                    return(parseValue);
                }
                else
                {
                    var key = this.value.Substring(2, this.value.Length - 3)?.Trim();

                    if (AutofacAnnotationModule.ComponentModelCache.TryGetValue(classType, out var component))
                    {
                        foreach (var metaSource in component.MetaSourceList)
                        {
                            if (metaSource.Configuration == null)
                            {
                                continue;
                            }
                            IConfigurationSection metData = metaSource.Configuration.GetSection(key);
                            var parameterValue            = ConfigurationUtil.GetConfiguredParameterValue(metData);

                            if (parameterValue == null)
                            {
                                //表示key不存在 从下一个source里面去寻找
                                continue;
                            }

                            var parseValue = parameterInfo == null
                                ? TypeManipulation.ChangeToCompatibleType(parameterValue, memberType, memberInfo)
                                : TypeManipulation.ChangeToCompatibleType(parameterValue, memberType, parameterInfo);

                            return(parseValue);
                        }
                    }
                }

                return(null);
            }
            catch (Exception ex)
            {
                throw new DependencyResolutionException($"Value set error,can not resolve class type:{classType.FullName} =====>" +
                                                        $" {(parameterInfo == null?memberType.Name:parameterInfo.Name)} "
                                                        + (!string.IsNullOrEmpty(this.value) ? $",with value:[{this.value}]" : ""), ex);
            }
        }