using System; using System.Collections.Generic; namespace ProtoCSStruct { public class ProtoMessageField { public FieldType FieldType; //如果是message或enum,类型的名字 public string TypeName; //字段名字 public string Name; public int Number; public bool IsRepeated; public int RepeatedCount; public int StringLength;//字符串长度 //当类型是enum的时候有效 public ProtoEnum Enum; //当类型是message的时候有效 public ProtoMessage Message; //cs里生成的Repeated类型名字 public string RepeatedFiledTypeNameInCSharp; public int RepeatedFiledBuffSize; public bool IsPrimitiveValueTypeField() { if(FieldType == FieldType.Int32 || FieldType == FieldType.UInt32 || FieldType == FieldType.Int64 || FieldType == FieldType.UInt64) { return true; } return false; } public string GetFiledTypeNameInCSharp() { if (FieldType == FieldType.Bool) { return "bool"; } if (FieldType == FieldType.Int32) { return "int"; } if (FieldType == FieldType.UInt32) { return "uint"; } if (FieldType == FieldType.Int64) { return "long"; } if (FieldType == FieldType.UInt64) { return "ulong"; } if (FieldType == FieldType.Bytes) { return "byte"; } if (FieldType == FieldType.String) { return FieldTypeUtils.GetFixedStringClassName(StringLength); } if(FieldType == FieldType.Enum || FieldType == FieldType.Message) { return TypeName; } ExceptionUtils.Throw("protocsstruct filed type error type {0} filed {1}" , TypeName, Name); return ""; } public string GetRepeatedFiledTypeNameInCSharp() { string repeatedTypeName = ""; if (FieldType == FieldType.Bool) { repeatedTypeName = "Bool"; } if (FieldType == FieldType.Int32) { repeatedTypeName = "Int"; } if (FieldType == FieldType.UInt32) { repeatedTypeName = "Uint"; } if (FieldType == FieldType.Int64) { repeatedTypeName = "Long"; } if (FieldType == FieldType.UInt64) { repeatedTypeName = "Ulong"; } if (FieldType == FieldType.Bytes) { repeatedTypeName = "Byte"; } if (FieldType == FieldType.Enum || FieldType == FieldType.Message) { repeatedTypeName = TypeName; } if (FieldType == FieldType.String) { repeatedTypeName = FieldTypeUtils.GetFixedStringClassName(StringLength); } RepeatedFiledTypeNameInCSharp = FieldTypeUtils.GetRepeatedMessageClassName(repeatedTypeName, RepeatedCount); return RepeatedFiledTypeNameInCSharp; } } }