当前位置: 首页>>代码示例>>C#>>正文


C# CodeWriter.Bracket方法代码示例

本文整理汇总了C#中CodeWriter.Bracket方法的典型用法代码示例。如果您正苦于以下问题:C# CodeWriter.Bracket方法的具体用法?C# CodeWriter.Bracket怎么用?C# CodeWriter.Bracket使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在CodeWriter的用法示例。


在下文中一共展示了CodeWriter.Bracket方法的8个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C#代码示例。

示例1: GenerateClassSerializer

        public static void GenerateClassSerializer(ProtoMessage m, CodeWriter cw)
        {
            if (m.OptionExternal || m.OptionType == "interface")
            {
                //Don't make partial class of external classes or interfaces
                //Make separate static class for them
                cw.Bracket(m.OptionAccess + " static class " + m.SerializerType);
            }
            else
            {
                cw.Attribute("System.Serializable()");
                cw.Bracket(m.OptionAccess + " partial " + m.OptionType + " " + m.SerializerType);
            }

            GenerateReader(m, cw);

            GenerateWriter(m, cw);
            foreach (ProtoMessage sub in m.Messages.Values)
            {
                cw.WriteLine();
                GenerateClassSerializer(sub, cw);
            }
            cw.EndBracket();
            cw.WriteLine();
            return;
        }
开发者ID:huodianyan,项目名称:ProtoBuf,代码行数:26,代码来源:MessageSerializer.cs

示例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();
 }
开发者ID:huodianyan,项目名称:ProtoBuf,代码行数:11,代码来源:MessageCode.cs

示例3: 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;
        }
开发者ID:huodianyan,项目名称:ProtoBuf,代码行数:43,代码来源:MessageCode.cs

示例4: 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;
//.........这里部分代码省略.........
开发者ID:huodianyan,项目名称:ProtoBuf,代码行数:101,代码来源:MessageSerializer.cs

示例5: 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();
        }
开发者ID:huodianyan,项目名称:ProtoBuf,代码行数:50,代码来源:MessageSerializer.cs

示例6: FieldWriter

        /// <summary>
        /// Generates code for writing one field
        /// </summary>
        public void FieldWriter(ProtoMessage m, Field f, CodeWriter cw, Options options)
        {
            try
            {
                cw.WriteLine("if (instance.ChangedProp.IndexOf(\"" + f.CsName + "\") >= 0)");
                cw.Bracket();
                if (f.Rule == FieldRule.Repeated)
                {
                    if (f.OptionPacked == true)
                    {
                        //Repeated packed
                        cw.IfBracket("instance." + f.CsName + " != null");

                        KeyWriter("stream", f.ID, Wire.LengthDelimited, cw);
                        if (f.ProtoType.WireSize < 0)
                        {
                            //Un-optimized, unknown size
                            cw.WriteLine("msField.SetLength(0);");
                            if (f.IsUsingBinaryWriter)
                                cw.WriteLine("BinaryWriter bw" + f.ID + " = new BinaryWriter(ms" + f.ID + ");");

                            cw.ForeachBracket("var i" + f.ID + " in instance." + f.CsName);
                            FieldWriterType(f, "msField", "bw" + f.ID, "i" + f.ID, cw);
                            cw.EndBracket();

                            BytesWriter(f, "stream", cw);
                        }
                        else
                        {
                            //Optimized with known size
                            //No memorystream buffering, write size first at once

                            //For constant size messages we can skip serializing to the MemoryStream
                            cw.WriteLine(ProtocolParser.Base + ".WriteUInt32(stream, " + f.ProtoType.WireSize + "u * (uint)instance." + f.CsName + ".Count);");

                            cw.ForeachBracket("var i" + f.ID + " in instance." + f.CsName);
                            FieldWriterType(f, "stream", "bw", "i" + f.ID, cw);
                            cw.EndBracket();
                        }
                        cw.EndBracket();
                    }
                    else
                    {
                        //Repeated not packet
                        cw.IfBracket("instance." + f.CsName + " != null");
                        cw.ForeachBracket("var i" + f.ID + " in instance." + f.CsName);
                        KeyWriter("stream", f.ID, f.ProtoType.WireType, cw);
                        FieldWriterType(f, "stream", "bw", "i" + f.ID, cw);
                        cw.EndBracket();
                        cw.EndBracket();
                    }
                    return;
                }
                else if (f.Rule == FieldRule.Optional)
                {
                    if (options.Nullable ||
                        f.ProtoType is ProtoMessage ||
                        f.ProtoType.ProtoName == ProtoBuiltin.String ||
                        f.ProtoType.ProtoName == ProtoBuiltin.Bytes)
                    {
                        if (f.ProtoType.Nullable || options.Nullable) //Struct always exist, not optional
                            cw.IfBracket("instance." + f.CsName + " != null");
                        KeyWriter("stream", f.ID, f.ProtoType.WireType, cw);
                        var needValue = !f.ProtoType.Nullable && options.Nullable;
                        FieldWriterType(f, "stream", "bw", "instance." + f.CsName + (needValue ? ".Value" : ""), cw);
                        if (f.ProtoType.Nullable || options.Nullable) //Struct always exist, not optional
                            cw.EndBracket();
                        return;
                    }
                    if (f.ProtoType is ProtoEnum)
                    {
                        if (f.OptionDefault != null)
                            cw.IfBracket("instance." + f.CsName + " != " + f.ProtoType.CsType + "." + f.OptionDefault);
                        KeyWriter("stream", f.ID, f.ProtoType.WireType, cw);
                        FieldWriterType(f, "stream", "bw", "instance." + f.CsName, cw);
                        if (f.OptionDefault != null)
                            cw.EndBracket();
                        return;
                    }
                    KeyWriter("stream", f.ID, f.ProtoType.WireType, cw);
                    FieldWriterType(f, "stream", "bw", "instance." + f.CsName, cw);
                    return;
                }
                else if (f.Rule == FieldRule.Required)
                {
                    if (f.ProtoType is ProtoMessage && f.ProtoType.OptionType != "struct" ||
                        f.ProtoType.ProtoName == ProtoBuiltin.String ||
                        f.ProtoType.ProtoName == ProtoBuiltin.Bytes)
                    {
                        cw.WriteLine("if (instance." + f.CsName + " == null)");
                        cw.WriteIndent("throw new global::SilentOrbit.ProtocolBuffers.ProtocolBufferException(\"" + f.CsName + " is required by the proto specification.\");");
                    }
                    KeyWriter("stream", f.ID, f.ProtoType.WireType, cw);
                    FieldWriterType(f, "stream", "bw", "instance." + f.CsName, cw);
                    return;
                }
            }
//.........这里部分代码省略.........
开发者ID:KennethLK,项目名称:ProtoBuf-1,代码行数:101,代码来源:FieldSerializer.cs

示例7: Save

        /// <summary>
        /// Generate code for reading and writing protocol buffer messages
        /// </summary>
        public static void Save(ProtoCollection file, Options options)
        {
            string csPath = options.OutputPath;
            CodeWriter.DefaultIndentPrefix = options.UseTabs ? "\t" : "    ";

            string ext = Path.GetExtension(csPath);
            string prefix = csPath.Substring(0, csPath.Length - ext.Length);

            string csDir = Path.GetDirectoryName(csPath);
            if (Directory.Exists(csDir) == false)
                Directory.CreateDirectory(csDir);

            //Basic structures
            using (var cw = new CodeWriter(csPath))
            {
                cw.Comment(@"Classes and structures being serialized");
                cw.WriteLine();
                cw.Comment(@"Generated by ProtocolBuffer
            - a pure c# code generation implementation of protocol buffers
            Report bugs to: https://silentorbit.com/protobuf/");
                cw.WriteLine();
                cw.Comment(@"DO NOT EDIT
            This file will be overwritten when CodeGenerator is run.
            To make custom modifications, edit the .proto file and add //:external before the message line
            then write the code and the changes in a separate file.");

                cw.WriteLine("using System;");
                cw.WriteLine("using System.Collections.Generic;");
                cw.WriteLine();

                string ns = null; //avoid writing namespace between classes if they belong to the same
                foreach (ProtoMessage m in file.Messages.Values)
                {
                    if (ns != m.CsNamespace)
                    {
                        if (ns != null) //First time
                            cw.EndBracket();
                        cw.Bracket("namespace " + m.CsNamespace);
                        ns = m.CsNamespace;
                    }
                    MessageCode.GenerateClass(m, cw, options);
                    cw.WriteLine();
                }

                foreach (ProtoEnum e in file.Enums.Values)
                {
                    if (ns != e.CsNamespace)
                    {
                        if (ns != null) //First time
                            cw.EndBracket();
                        cw.Bracket("namespace " + e.CsNamespace);
                        ns = e.CsNamespace;
                    }
                    MessageCode.GenerateEnum(e, cw);
                    cw.WriteLine();
                }

                cw.EndBracket();
            }

            //.Serializer.cs
            //Code for Reading/Writing
            using (var cw = new CodeWriter(prefix + ".Serializer" + ext))
            {
                cw.Comment(@"This is the backend code for reading and writing");
                cw.WriteLine();
                cw.Comment(@"Generated by ProtocolBuffer
            - a pure c# code generation implementation of protocol buffers
            Report bugs to: https://silentorbit.com/protobuf/");
                cw.WriteLine();
                cw.Comment(@"DO NOT EDIT
            This file will be overwritten when CodeGenerator is run.");

                cw.WriteLine("using System;");
                cw.WriteLine("using System.IO;");
                cw.WriteLine("using System.Text;");
                cw.WriteLine("using System.Collections.Generic;");
                cw.WriteLine();

                string ns = null; //avoid writing namespace between classes if they belong to the same
                foreach (ProtoMessage m in file.Messages.Values)
                {
                    if (ns != m.CsNamespace)
                    {
                        if (ns != null) //First time
                            cw.EndBracket();
                        cw.Bracket("namespace " + m.CsNamespace);
                        ns = m.CsNamespace;
                    }
                    MessageSerializer.GenerateClassSerializer(m, cw);
                }
                cw.EndBracket();
            }

            string libPath = Path.Combine(Path.GetDirectoryName(csPath), "ProtocolParser.cs");
            using (TextWriter codeWriter = new StreamWriter(libPath, false, Encoding.UTF8))
            {
//.........这里部分代码省略.........
开发者ID:huodianyan,项目名称:ProtoBuf,代码行数:101,代码来源:ProtoCode.cs

示例8: FieldSizeWriterType

        void FieldSizeWriterType(Field f, string instance, CodeWriter cw)
        {
            if (f.OptionCodeType != null)
                Console.WriteLine("not support");

            cw.Bracket();
            cw.WriteLine(FieldSizeWriterPrimitive(f, instance));
            cw.EndBracket();
        }
开发者ID:slb1988,项目名称:StrangeIoCLearning,代码行数:9,代码来源:FieldSerializer.cs


注:本文中的CodeWriter.Bracket方法示例由纯净天空整理自Github/MSDocs等开源代码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。