private void CheckFieldName(string name)
        {
            int computedHash = JenkinsHash.GetHashCode(name);
            int hash         = this.reader.ReadInt32();

            if (computedHash != hash)
            {
                throw new InvalidOperationException("The field hash is not what was expected...");
            }
        }
Exemple #2
0
 /// <summary>
 /// Serializes null for the specified variable of the specified type.
 /// </summary>
 /// <param name="name">Name of the variable to serialize</param>
 /// <param name="type">Type to serialize for</param>
 public void SerializeNull(string name, Type type)
 {
     this.AssertNotDisposed();
     this.writer.Write(JenkinsHash.GetHashCode(name + type.Name));
     this.writer.Write(false);
 }
Exemple #3
0
        /// <summary>
        /// Serializes a value for the specified variable.
        /// </summary>
        /// <typeparam name="T">Type of variable to serialize</typeparam>
        /// <param name="name">Name of variable</param>
        /// <param name="value">Value to serialize</param>
        public void Serialize <T>(string name, T value)
        {
            this.AssertNotDisposed();
            this.writer.Write(JenkinsHash.GetHashCode(name + typeof(T).Name));
            if (typeof(T) == typeof(byte))
            {
                this.writer.Write((byte)SerializationFlags.NotNull);
                this.writer.Write((byte)(object)value);
                return;
            }

            if (typeof(T) == typeof(char))
            {
                this.writer.Write((byte)SerializationFlags.NotNull);
                this.writer.Write((char)(object)value);
                return;
            }

            if (typeof(T) == typeof(bool))
            {
                this.writer.Write((byte)SerializationFlags.NotNull);
                this.writer.Write((bool)(object)value);
                return;
            }

            if (typeof(T) == typeof(short))
            {
                this.writer.Write((byte)SerializationFlags.NotNull);
                this.writer.Write((short)(object)value);
                return;
            }

            if (typeof(T) == typeof(int))
            {
                this.writer.Write((byte)SerializationFlags.NotNull);
                this.writer.Write((int)(object)value);
                return;
            }

            if (typeof(T) == typeof(long))
            {
                this.writer.Write((byte)SerializationFlags.NotNull);
                this.writer.Write((long)(object)value);
                return;
            }

            if (typeof(T) == typeof(float))
            {
                this.writer.Write((byte)SerializationFlags.NotNull);
                this.writer.Write((float)(object)value);
                return;
            }

            if (typeof(T) == typeof(double))
            {
                this.writer.Write((byte)SerializationFlags.NotNull);
                this.writer.Write((double)(object)value);
                return;
            }

            if (typeof(T) == typeof(decimal))
            {
                this.writer.Write((byte)SerializationFlags.NotNull);
                this.writer.Write((decimal)(object)value);
                return;
            }

            if (typeof(T) == typeof(DateTime))
            {
                this.writer.Write((byte)SerializationFlags.NotNull);
                this.writer.Write(((DateTime)(object)value).Ticks);
                return;
            }

            if (typeof(T) == typeof(DateTimeOffset))
            {
                this.writer.Write((byte)SerializationFlags.NotNull);
                this.writer.Write(((DateTimeOffset)(object)value).Ticks);
                this.writer.Write(((DateTimeOffset)(object)value).Offset.Ticks);
                return;
            }

            if (typeof(T) == typeof(string))
            {
                this.writer.Write((byte)SerializationFlags.NotNull);
                var bytes = Encoding.UTF8.GetBytes((string)(object)value);
                this.writer.Write(bytes.Length);
                this.writer.Write(bytes, 0, bytes.Length);
                return;
            }

            if (typeof(T) == typeof(Type))
            {
                this.writer.Write((byte)SerializationFlags.NotNull);
                this.writer.Write(((Type)(object)value).AssemblyQualifiedName);
                return;
            }

            if (SerializationInfo <T> .IsSerializable)
            {
                this.Serialize(value);
            }
            else if (SerializationInfo <T> .IsProtoBuf)
            {
                this.ProtoBuf(value);
            }
            else if (SerializationInfo <T> .IsFormattable)
            {
                this.Formatter(value);
            }
            else
            {
                throw new NotSupportedException("The type is not serializable and is not a protobuf contract.");
            }
        }