public WechatPayConfig GetConfig(string name)
        {
            WechatPayConfig WechatPayConfig = null;

            if (!dic.TryGetValue(name, out WechatPayConfig))
            {
                throw new Exception($"不存在{name}该配置");
            }

            return(WechatPayConfig);
        }
Example #2
0
        public TWehcatService GetService <TWehcatService>(WechatPayConfig wechatPayConfig) where TWehcatService : class
        {
            wechatPayConfig.CheckNull(nameof(WechatPayConfig));
            var service = _serviceProvider.GetService(typeof(TWehcatService)) as TWehcatService;

            if (service == null)
            {
                throw new Exception($"未找到服务{typeof(TWehcatService).FullName}的实现");
            }
            if (service is IWechatConfigSetter wechatConfigSetter)
            {
                wechatConfigSetter.SetConfig(wechatPayConfig);
            }
            else
            {
                var methodType = service.GetType().GetMethod("SetConfig");
                if (methodType == null)
                {
                    throw new Exception($"未找到方法SetConfig");
                }
                if (methodType.IsStatic || methodType.IsAbstract)
                {
                    throw new Exception($"未找到非静态或非抽象的方法SetConfig");
                }
                IList <object> args = new List <object>();
                bool           flag = false;
                foreach (var item in methodType.GetParameters())
                {
                    if (item.HasDefaultValue)
                    {
                        args.Add(item.DefaultValue);
                    }
                    else if (item.ParameterType == typeof(WechatPayConfig))
                    {
                        flag = true;
                        args.Add(wechatPayConfig);
                    }
                    else if (item.IsOptional)
                    {
                    }
                    else
                    {
                        var arg = _serviceProvider.GetService(item.ParameterType);
                        args.Add(arg);
                    }
                }
                if (!flag)
                {
                    throw new Exception($"方法SetConfig缺少WechatPayConfig参数");
                }
                methodType.Invoke(service, args.ToArray());
            }
            return(service);
        }
 public void AddWechatPayConfig(string name, WechatPayConfig WechatPayConfig)
 {
     WechatPayConfig.Validate();
     dic.TryAdd(name, WechatPayConfig);
 }