Пример #1
0
 /// <summary>
 /// 创建Api实例
 /// </summary>
 public ApiBase()
 {
     Params = new ParamCollection();
     AddParamDefinition(new ParamDefinition("appid", true, 1, 32));
     AddParamDefinition(new ParamDefinition("mch_id", true, 1, 32));
     AddParamDefinition(new ParamDefinition("sub_appid", false, 1, 32));
     AddParamDefinition(new ParamDefinition("sub_mch_id", false, 1, 32));
     AddParamDefinition(new ParamDefinition("nonce_str", true, 1, 32));
     AddParamDefinition(new ParamDefinition("sign", true, 1, 32));
     AddParamDefinition(new ParamDefinition("sign_type", false, 0, 32));
     AddParamDefinition(new ParamDefinition("key", true, 1, 256));
     TSetNonceStr();
 }
 /// <summary>
 /// 用本定义集合检测指定的参数集合是否合法
 /// </summary>
 /// <param name="params"></param>
 public void Test(ParamCollection @params)
 {
     foreach (var item in _inner)
     {
         var _bool = @params.ContainsKey(item.Name);
         var _v    = _bool ? @params[item.Name] : null;
         if (item.Requierd)
         {
             if (!_bool)
             {
                 var _selection = _selections.FirstOrDefault(t => t.Count(m => m.Name == item.Name) > 0);
                 if (_selection == null)
                 {
                     throw new Exception($"参数\"{item.Name}\"是必须的");
                 }
                 else
                 {
                     if (_selection.Count(t => @params.ContainsKey(t.Name)) == 0)
                     {
                         throw new Exception($"参数{string.Join(",", _selection.Select((v, i) => v.Name))}必须指定一个");
                     }
                     else
                     {
                         continue;
                     }
                 }
             }
         }
         if (item.Test(_v))
         {
             continue;
         }
         else
         {
             throw new Exception($"值\"{_v}\"对于参数\"{item.Name}\"是不合法的");
         }
     }
 }