/// <summary> /// 解析为枚举项数据 /// </summary> /// <param name="name">枚举定义名称</param> /// <param name="description">枚举定义描述</param> /// <param name="defRaw">原始定义字符串</param> /// <returns></returns> public static EnumContract Parse(string name, string description, string defRaw) { EnumContract enc = new EnumContract(); //EaseSuccessFlag = short=>{Error:-1, UnKnown:0, Success:1010, SuccessUserAgent:1020, SuccessExpress:1120} //#服务器响应码(short 3.2-2.6.9) //StatusCode=short=>{Exception:-1/*服务器异常*/, Success:0/*服务器处理成功*/, Updatable:1/*主程序有更新,请按照客户端更新策略下载*/} enc.TypeName = name; enc.Description = description; int idx = defRaw.IndexOf("=>"); if (idx == -1) { throw new SpecDataDefineException("没有找到枚举数据类型定义的分隔标识符号:'=>'!"); } enc.BaseType = defRaw.Substring(0, idx).Trim(); string itemDefExp = defRaw.Substring(idx + 2).Trim().Trim('{', '}'); #region 最简定义解析 string[] itemArr = itemDefExp.Split(new char[] { ',' }, StringSplitOptions.RemoveEmptyEntries); string key = null, value = null; foreach (string item in itemArr) { idx = item.IndexOf(':'); if (idx == -1) { continue; } else { key = item.Substring(0, idx).Trim(); value = item.Substring(idx + 1); //检查是否包含注释 idx = value.IndexOf('/'); if (idx == -1) { enc.SetEnumItem(key, value); } else { enc[key] = new DataItem { DataName = key, ItemValue = value.Substring(0, idx).Trim(), Description = value.Substring(idx).Trim('/', '*') }; } } } #endregion #region 灵活定义解析 //using (StringParseHelper dep = new StringParseHelper(itemDefExp)) //{ // //{Exception:-1/*服务器异常*/, Success:0/*服务器处理成功*/, Updatable:1/*主程序有更新,请按照客户端更新策略下载*/} // /* // * when found : => getBufferAsKey -> readToChar([',','}']) -> getBufferAsValue // * when found / => checkNextCharIs('*') -> readToChar(['*']) and peekNextChar == '/' as Key Comment // * // */ // dep.Parse(); //} #endregion return(enc); }