示例#1
0
 public static byte[] ToByteArray(this IMessage message)
 {
     Preconditions.CheckNotNull(message, "message");
     byte[] result = new byte[message.CalculateSize()];
     CodedOutputStream output = CodedOutputStream.CreateInstance(result);
     message.WriteTo(output);
     output.CheckNoSpaceLeft();
     return result;
 }
示例#2
0
 /// <summary>
 /// Writes the length and then data of the given message to a stream.
 /// </summary>
 /// <param name="message">The message to write.</param>
 /// <param name="output">The output stream to write to.</param>
 public static void WriteDelimitedTo(this IMessage message, Stream output)
 {
     ProtoPreconditions.CheckNotNull(message, "message");
     ProtoPreconditions.CheckNotNull(output, "output");
     CodedOutputStream codedOutput = new CodedOutputStream(output);
     codedOutput.WriteRawVarint32((uint)message.CalculateSize());
     message.WriteTo(codedOutput);
     codedOutput.Flush();
 }
示例#3
0
        /// <summary>
        /// </summary>
        /// <param name="type">
        /// </param>
        /// <param name="asValueType">
        /// </param>
        /// <returns>
        /// </returns>
        public static int GetTypeSize(this IType type, bool asValueType = false)
        {
            if (asValueType && type.IsPrimitiveType())
            {
                return SystemTypeSizes[type.Name];
            }

            // find index
            int size;
            if (!sizeByType.TryGetValue(type, out size))
            {
                IList<MemberLocationInfo> membersLayout;
                size = type.CalculateSize(out membersLayout);
                sizeByType[type] = size;
                membersLayoutByType[type] = membersLayout;
            }

            return size;
        }