本文整理汇总了C#中CodeWriter.WhileBracket方法的典型用法代码示例。如果您正苦于以下问题:C# CodeWriter.WhileBracket方法的具体用法?C# CodeWriter.WhileBracket怎么用?C# CodeWriter.WhileBracket使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类CodeWriter
的用法示例。
在下文中一共展示了CodeWriter.WhileBracket方法的2个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C#代码示例。
示例1: GenerateReader
//.........这里部分代码省略.........
{
if (f.ProtoType is ProtoEnum)
cw.WriteLine("instance." + f.CsName + " = " + f.ProtoType.FullCsType + "." + f.OptionDefault + ";");
else
cw.WriteLine("instance." + f.CsName + " = " + f.OptionDefault + ";");
}
else if (f.Rule == FieldRule.Optional)
{
if (f.ProtoType is ProtoEnum)
{
ProtoEnum pe = f.ProtoType as ProtoEnum;
//the default value is the first value listed in the enum's type definition
foreach (var kvp in pe.Enums)
{
cw.WriteLine("instance." + f.CsName + " = " + pe.FullCsType + "." + kvp.Name + ";");
break;
}
}
}
}
if (method == "DeserializeLengthDelimited")
{
//Important to read stream position after we have read the length field
cw.WriteLine("long limit = global::SilentOrbit.ProtocolBuffers.ProtocolParser.ReadUInt32(stream);");
cw.WriteLine("limit += stream.Position;");
}
if (method == "DeserializeLength")
{
//Important to read stream position after we have read the length field
cw.WriteLine("long limit = stream.Position + length;");
}
cw.WhileBracket("true");
if (method == "DeserializeLengthDelimited" || method == "DeserializeLength")
{
cw.IfBracket("stream.Position >= limit");
cw.WriteLine("if (stream.Position == limit)");
cw.WriteIndent("break;");
cw.WriteLine("else");
cw.WriteIndent("throw new InvalidOperationException(\"Read past max limit\");");
cw.EndBracket();
}
cw.WriteLine("int keyByte = stream.ReadByte();");
cw.WriteLine("if (keyByte == -1)");
if (method == "Deserialize")
cw.WriteIndent("break;");
else
cw.WriteIndent("throw new System.IO.EndOfStreamException();");
//Determine if we need the lowID optimization
bool hasLowID = false;
foreach (Field f in m.Fields.Values)
{
if (f.ID < 16)
{
hasLowID = true;
break;
}
}
if (hasLowID)
{
cw.Comment("Optimized reading of known fields with field ID < 16");
示例2: FieldReader
/// <summary>
/// Return true for normal code and false if generated thrown exception.
/// In the latter case a break is not needed to be generated afterwards.
/// </summary>
public static bool FieldReader(Field f, CodeWriter cw)
{
if (f.Rule == FieldRule.Repeated)
{
//Make sure we are not reading a list of interfaces
if (f.ProtoType.OptionType == "interface")
{
cw.WriteLine("throw new InvalidOperationException(\"Can't deserialize a list of interfaces\");");
return false;
}
if (f.OptionPacked == true)
{
cw.Comment("repeated packed");
cw.WriteLine("long end" + f.ID + " = global::SilentOrbit.ProtocolBuffers.ProtocolParser.ReadUInt32(stream);");
cw.WriteLine("end" + f.ID + " += stream.Position;");
cw.WhileBracket("stream.Position < end" + f.ID);
cw.WriteLine("instance." + f.CsName + ".Add(" + FieldReaderType(f, "stream", "br", null) + ");");
cw.EndBracket();
cw.WriteLine("if (stream.Position != end" + f.ID + ")");
cw.WriteIndent("throw new InvalidDataException(\"Read too many bytes in packed data\");");
}
else
{
cw.Comment("repeated");
cw.WriteLine("instance." + f.CsName + ".Add(" + FieldReaderType(f, "stream", "br", null) + ");");
}
}
else
{
if (f.OptionReadOnly)
{
//The only "readonly" fields we can modify
//We could possibly support bytes primitive too but it would require the incoming length to match the wire length
if (f.ProtoType is ProtoMessage)
{
cw.WriteLine(FieldReaderType(f, "stream", "br", "instance." + f.CsName) + ";");
return true;
}
cw.WriteLine("throw new InvalidOperationException(\"Can't deserialize into a readonly primitive field\");");
return false;
}
if (f.ProtoType is ProtoMessage)
{
if (f.ProtoType.OptionType == "struct")
{
cw.WriteLine(FieldReaderType(f, "stream", "br", "ref instance." + f.CsName) + ";");
return true;
}
cw.WriteLine("if (instance." + f.CsName + " == null)");
if (f.ProtoType.OptionType == "interface")
cw.WriteIndent("throw new InvalidOperationException(\"Can't deserialize into a interfaces null pointer\");");
else
cw.WriteIndent("instance." + f.CsName + " = " + FieldReaderType(f, "stream", "br", null) + ";");
cw.WriteLine("else");
cw.WriteIndent(FieldReaderType(f, "stream", "br", "instance." + f.CsName) + ";");
return true;
}
cw.WriteLine("instance." + f.CsName + " = " + FieldReaderType(f, "stream", "br", "instance." + f.CsName) + ";");
}
return true;
}