コード例 #1
0
    /// <summary>
    /// 解析协议字段
    /// </summary>
    /// <param name="content"></param>
    /// <returns></returns>
    public static List <ProtoParamData> GetParameters(string content)
    {
        List <ProtoParamData> paramList = new List <ProtoParamData>();
        var paramArr = content.Split(new string[] { "\r\n" }, StringSplitOptions.RemoveEmptyEntries);

        foreach (var p in paramArr)
        {
            if (!p.Contains(":"))
            {
                continue;
            }
            ProtoParamData paramData = new ProtoParamData();
            paramData.isArr = false;
            string con = p.Trim();
            if (p.Contains("#"))//去掉注释
            {
                con = con.Split('#')[0].Trim();
            }
            var nameOrType = con.Split(':');
            //处理名字
            string name = nameOrType[0];
            string type = nameOrType[1].Trim();
            name = RemoveStrNumber(name).Trim(); //利用正则过滤掉序号
            if (type.Contains("*"))              //是数组或字典
            {
                if (type.Contains("("))          //字典
                {
                    var typeArr = type.Split('(');
                    type = typeArr[0].Trim();
                }
                type            = type.Replace("*", "");
                paramData.isArr = true;
            }
            paramData.name = name;
            paramData.type = type;
            paramList.Add(paramData);
        }
        return(paramList);
    }
コード例 #2
0
    /// <summary>
    /// 设置协议参数默认值并返回
    /// </summary>
    public static string SetParametersDefaultValue(ProtoParamData param)
    {
        string str  = "\t\t{0} = {1},\n";//string.Format("\t\t{0} = nil,\n", param.name);
        string type = "nil";

        if (!param.isArr)
        {
            if (param.type.Equals("integer"))
            {
                type = "0";
            }
            else if (param.type.Equals("string"))
            {
                type = "\"\"";
            }
            else if (param.type.Equals("boolean"))
            {
                type = "false";
            }
        }

        str = string.Format(str, param.name, type);
        return(str);
    }