本文整理汇总了C#中CodeWriter.Summary方法的典型用法代码示例。如果您正苦于以下问题:C# CodeWriter.Summary方法的具体用法?C# CodeWriter.Summary怎么用?C# CodeWriter.Summary使用的例子?那么, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类CodeWriter
的用法示例。
在下文中一共展示了CodeWriter.Summary方法的5个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C#代码示例。
示例1: GenerateClass
public static void GenerateClass(ProtoMessage m, CodeWriter cw, Options options)
{
//Do not generate class code for external classes
if (m.OptionExternal)
{
cw.Comment("Written elsewhere");
cw.Comment(m.OptionAccess + " " + m.OptionType + " " + m.CsType + " {}");
return;
}
//Default class
cw.Summary(m.Comments);
cw.Bracket(m.OptionAccess + " partial " + m.OptionType + " " + m.CsType);
GenerateEnums(m, cw);
GenerateProperties(m, cw);
//if(options.GenerateToString...
// ...
if (m.OptionPreserveUnknown)
{
cw.Summary("Values for unknown fields.");
cw.WriteLine("public List<global::SilentOrbit.ProtocolBuffers.KeyValue> PreservedFields;");
cw.WriteLine();
}
if (m.OptionTriggers)
{
cw.Comment("protected virtual void BeforeSerialize() {}");
cw.Comment("protected virtual void AfterDeserialize() {}");
cw.WriteLine();
}
foreach (ProtoMessage sub in m.Messages.Values)
{
GenerateClass(sub, cw, options);
cw.WriteLine();
}
cw.EndBracket();
return;
}
示例2: GenerateEnum
public static void GenerateEnum(ProtoEnum me, CodeWriter cw)
{
cw.Bracket("public enum " + me.CsType);
foreach (var epair in me.Enums)
{
cw.Summary(epair.Comment);
cw.WriteLine(epair.Name + " = " + epair.Value + ",");
}
cw.EndBracket();
cw.WriteLine();
}
示例3: GenerateReader
static void GenerateReader(ProtoMessage m, CodeWriter cw)
{
#region Helper Deserialize Methods
string refstr = (m.OptionType == "struct") ? "ref " : "";
if (m.OptionType != "interface")
{
cw.Summary("Helper: create a new instance to deserializing into");
cw.Bracket(m.OptionAccess + " static " + m.CsType + " Deserialize(Stream stream)");
cw.WriteLine(m.CsType + " instance = new " + m.CsType + "();");
cw.WriteLine("Deserialize(stream, " + refstr + "instance);");
cw.WriteLine("return instance;");
cw.EndBracketSpace();
cw.Summary("Helper: create a new instance to deserializing into");
cw.Bracket(m.OptionAccess + " static " + m.CsType + " DeserializeLengthDelimited(Stream stream)");
cw.WriteLine(m.CsType + " instance = new " + m.CsType + "();");
cw.WriteLine("DeserializeLengthDelimited(stream, " + refstr + "instance);");
cw.WriteLine("return instance;");
cw.EndBracketSpace();
cw.Summary("Helper: create a new instance to deserializing into");
cw.Bracket(m.OptionAccess + " static " + m.CsType + " DeserializeLength(Stream stream, int length)");
cw.WriteLine(m.CsType + " instance = new " + m.CsType + "();");
cw.WriteLine("DeserializeLength(stream, length, " + refstr + "instance);");
cw.WriteLine("return instance;");
cw.EndBracketSpace();
cw.Summary("Helper: put the buffer into a MemoryStream and create a new instance to deserializing into");
cw.Bracket(m.OptionAccess + " static " + m.CsType + " Deserialize(byte[] buffer)");
cw.WriteLine(m.CsType + " instance = new " + m.CsType + "();");
cw.WriteLine("using (var ms = new MemoryStream(buffer))");
cw.WriteIndent("Deserialize(ms, " + refstr + "instance);");
cw.WriteLine("return instance;");
cw.EndBracketSpace();
}
cw.Summary("Helper: put the buffer into a MemoryStream before deserializing");
cw.Bracket(m.OptionAccess + " static " + m.FullCsType + " Deserialize(byte[] buffer, " + refstr + m.FullCsType + " instance)");
cw.WriteLine("using (var ms = new MemoryStream(buffer))");
cw.WriteIndent("Deserialize(ms, " + refstr + "instance);");
cw.WriteLine("return instance;");
cw.EndBracketSpace();
#endregion
string[] methods = new string[]
{
"Deserialize", //Default old one
"DeserializeLengthDelimited", //Start by reading length prefix and stay within that limit
"DeserializeLength", //Read at most length bytes given by argument
};
//Main Deserialize
foreach (string method in methods)
{
if (method == "Deserialize")
{
cw.Summary("Takes the remaining content of the stream and deserialze it into the instance.");
cw.Bracket(m.OptionAccess + " static " + m.FullCsType + " " + method + "(Stream stream, " + refstr + m.FullCsType + " instance)");
}
else if (method == "DeserializeLengthDelimited")
{
cw.Summary("Read the VarInt length prefix and the given number of bytes from the stream and deserialze it into the instance.");
cw.Bracket(m.OptionAccess + " static " + m.FullCsType + " " + method + "(Stream stream, " + refstr + m.FullCsType + " instance)");
}
else if (method == "DeserializeLength")
{
cw.Summary("Read the given number of bytes from the stream and deserialze it into the instance.");
cw.Bracket(m.OptionAccess + " static " + m.FullCsType + " " + method + "(Stream stream, int length, " + refstr + m.FullCsType + " instance)");
}
else
throw new NotImplementedException();
if (m.IsUsingBinaryWriter)
cw.WriteLine("BinaryReader br = new BinaryReader(stream);");
//Prepare List<> and default values
foreach (Field f in m.Fields.Values)
{
if (f.Rule == FieldRule.Repeated)
{
//Initialize lists of the custom DateTime or TimeSpan type.
string csType = f.ProtoType.FullCsType;
if (f.OptionCodeType != null)
csType = f.OptionCodeType;
cw.WriteLine("if (instance." + f.CsName + " == null)");
cw.WriteIndent("instance." + f.CsName + " = new List<" + csType + ">();");
}
else if (f.OptionDefault != null)
{
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;
//.........这里部分代码省略.........
示例4: GenerateWriter
/// <summary>
/// Generates code for writing a class/message
/// </summary>
static void GenerateWriter(ProtoMessage m, CodeWriter cw)
{
cw.Summary("Serialize the instance into the stream");
cw.Bracket(m.OptionAccess + " static void Serialize(Stream stream, " + m.CsType + " instance)");
if (m.OptionTriggers)
{
cw.WriteLine("instance.BeforeSerialize();");
cw.WriteLine();
}
if (m.IsUsingBinaryWriter)
cw.WriteLine("BinaryWriter bw = new BinaryWriter(stream);");
//Shared memorystream for all fields
cw.Using("var msField = new MemoryStream(" + m.MaxFieldBufferSize() + ")");
foreach (Field f in m.Fields.Values)
FieldSerializer.FieldWriter(m, f, cw);
cw.EndBracket();
if (m.OptionPreserveUnknown)
{
cw.IfBracket("instance.PreservedFields != null");
cw.ForeachBracket("var kv in instance.PreservedFields");
cw.WriteLine("global::SilentOrbit.ProtocolBuffers.ProtocolParser.WriteKey(stream, kv.Key);");
cw.WriteLine("stream.Write(kv.Value, 0, kv.Value.Length);");
cw.EndBracket();
cw.EndBracket();
}
cw.EndBracket();
cw.WriteLine();
cw.Summary("Helper: Serialize into a MemoryStream and return its byte array");
cw.Bracket(m.OptionAccess + " static byte[] SerializeToBytes(" + m.CsType + " instance)");
cw.Using("var ms = new MemoryStream()");
cw.WriteLine("Serialize(ms, instance);");
cw.WriteLine("return ms.ToArray();");
cw.EndBracket();
cw.EndBracket();
cw.Summary("Helper: Serialize with a varint length prefix");
cw.Bracket(m.OptionAccess + " static void SerializeLengthDelimited(Stream stream, " + m.CsType + " instance)");
cw.WriteLine("var data = SerializeToBytes(instance);");
cw.WriteLine("global::SilentOrbit.ProtocolBuffers.ProtocolParser.WriteUInt32(stream, (uint)data.Length);");
cw.WriteLine("stream.Write(data, 0, data.Length);");
cw.EndBracket();
}
示例5: GenerateProperties
/// <summary>
/// Generates the properties.
/// </summary>
/// <param name='template'>
/// if true it will generate only properties that are not included by default, because of the [generate=false] option.
/// </param>
static void GenerateProperties(ProtoMessage m, CodeWriter cw)
{
foreach (Field f in m.Fields.Values)
{
if (f.OptionExternal)
cw.WriteLine("//" + GenerateProperty(f) + " // Implemented by user elsewhere");
else
{
if (f.Comments != null)
cw.Summary(f.Comments);
cw.WriteLine(GenerateProperty(f));
cw.WriteLine();
}
}
//Wire format field ID
#if DEBUG
cw.Comment("ProtocolBuffers wire field id");
foreach (Field f in m.Fields.Values)
{
cw.WriteLine("public const int " + f.CsName + "FieldID = " + f.ID + ";");
}
#endif
}