static ByteString EncodeObject (object value, MemoryStream buffer, CodedOutputStream stream) { buffer.SetLength (0); if (value == null) { stream.WriteUInt64 (0); } else if (value is Enum) { stream.WriteInt32 ((int)value); } else { Type type = value.GetType (); switch (Type.GetTypeCode (type)) { case TypeCode.Int32: stream.WriteInt32 ((int)value); break; case TypeCode.Int64: stream.WriteInt64 ((long)value); break; case TypeCode.UInt32: stream.WriteUInt32 ((uint)value); break; case TypeCode.UInt64: stream.WriteUInt64 ((ulong)value); break; case TypeCode.Single: stream.WriteFloat ((float)value); break; case TypeCode.Double: stream.WriteDouble ((double)value); break; case TypeCode.Boolean: stream.WriteBool ((bool)value); break; case TypeCode.String: stream.WriteString ((string)value); break; default: if (type == typeof(byte[])) stream.WriteBytes (ByteString.CopyFrom ((byte[])value)); else if (TypeUtils.IsAClassType (type)) stream.WriteUInt64 (ObjectStore.Instance.AddInstance (value)); else if (TypeUtils.IsAMessageType (type)) WriteMessage (value, stream); else if (TypeUtils.IsAListCollectionType (type)) WriteList (value, stream); else if (TypeUtils.IsADictionaryCollectionType (type)) WriteDictionary (value, stream); else if (TypeUtils.IsASetCollectionType (type)) WriteSet (value, stream); else if (TypeUtils.IsATupleCollectionType (type)) WriteTuple (value, stream); else throw new ArgumentException (type + " is not a serializable type"); break; } } stream.Flush (); return ByteString.CopyFrom (buffer.GetBuffer (), 0, (int)buffer.Length); }
public void WriteTo(CodedOutputStream output) { if (UninterpretedOption.Types.NamePart.smethod_1(this.NamePart_) != 0) { goto IL_65; } goto IL_9B; uint arg_6F_0; while (true) { IL_6A: uint num; switch ((num = (arg_6F_0 ^ 2196724152u)) % 5u) { case 0u: goto IL_65; case 1u: goto IL_9B; case 2u: output.WriteRawTag(10); output.WriteString(this.NamePart_); arg_6F_0 = (num * 3710217040u ^ 1616946663u); continue; case 4u: output.WriteRawTag(16); output.WriteBool(this.IsExtension); arg_6F_0 = (num * 4058525509u ^ 2603311045u); continue; } break; } return; IL_65: arg_6F_0 = 2475431821u; goto IL_6A; IL_9B: arg_6F_0 = ((!this.IsExtension) ? 4134694002u : 2434990451u); goto IL_6A; }
static ByteString EncodeObject (object value, Type type, MemoryStream buffer, CodedOutputStream stream) { buffer.SetLength (0); if (value != null && !type.IsInstanceOfType (value)) throw new ArgumentException ("Value of type " + value.GetType () + " cannot be encoded to type " + type); if (value == null && !type.IsSubclassOf (typeof(RemoteObject)) && !IsACollectionType (type)) throw new ArgumentException ("null cannot be encoded to type " + type); if (value == null) stream.WriteUInt64 (0); else if (value is Enum) stream.WriteInt32 ((int)value); else { switch (Type.GetTypeCode (type)) { case TypeCode.Int32: stream.WriteInt32 ((int)value); break; case TypeCode.Int64: stream.WriteInt64 ((long)value); break; case TypeCode.UInt32: stream.WriteUInt32 ((uint)value); break; case TypeCode.UInt64: stream.WriteUInt64 ((ulong)value); break; case TypeCode.Single: stream.WriteFloat ((float)value); break; case TypeCode.Double: stream.WriteDouble ((double)value); break; case TypeCode.Boolean: stream.WriteBool ((bool)value); break; case TypeCode.String: stream.WriteString ((string)value); break; default: if (type.Equals (typeof(byte[]))) stream.WriteBytes (ByteString.CopyFrom ((byte[])value)); else if (IsAClassType (type)) stream.WriteUInt64 (((RemoteObject)value).id); else if (IsAMessageType (type)) ((IMessage)value).WriteTo (buffer); else if (IsAListType (type)) WriteList (value, type, buffer); else if (IsADictionaryType (type)) WriteDictionary (value, type, buffer); else if (IsASetType (type)) WriteSet (value, type, buffer); else if (IsATupleType (type)) WriteTuple (value, type, buffer); else throw new ArgumentException (type + " is not a serializable type"); break; } } stream.Flush (); return ByteString.CopyFrom (buffer.GetBuffer (), 0, (int)buffer.Length); }
/// <summary> /// Encode a value of the given type. /// Should not be called directly. This interface is used by service client stubs. /// </summary> public static ByteString Encode(object value, Type type) { var stream = new MemoryStream (); var encoder = new CodedOutputStream (stream); if (value != null && !type.IsAssignableFrom (value.GetType ())) //TODO: nulls? throw new ArgumentException ("Value of type " + value.GetType () + " cannot be encoded to type " + type); if (type == typeof(Double)) encoder.WriteDouble ((Double)value); else if (type == typeof(Single)) encoder.WriteFloat ((Single)value); else if (type == typeof(Int32)) encoder.WriteInt32 ((Int32)value); else if (type == typeof(Int64)) encoder.WriteInt64 ((Int64)value); else if (type == typeof(UInt32)) encoder.WriteUInt32 ((UInt32)value); else if (type == typeof(UInt64)) encoder.WriteUInt64 ((UInt64)value); else if (type == typeof(Boolean)) encoder.WriteBool ((Boolean)value); else if (type == typeof(String)) encoder.WriteString ((String)value); else if (type == typeof(byte[])) encoder.WriteBytes (ByteString.CopyFrom ((byte[])value)); else if (value != null && value is Enum) encoder.WriteInt32 ((int)value); else if (type.IsSubclassOf (typeof(RemoteObject))) { if (value == null) encoder.WriteUInt64 (0); else encoder.WriteUInt64 (((RemoteObject)value)._ID); } else if (value != null && value is IMessage) { ((IMessage)value).WriteTo (stream); return ByteString.CopyFrom (stream.ToArray ()); } else if (value != null && value is IList) return EncodeList (value, type); else if (value != null && value is IDictionary) return EncodeDictionary (value, type); else if (value != null && value.GetType ().IsGenericType && value.GetType ().GetGenericTypeDefinition () == typeof(HashSet<>)) return EncodeSet (value, type); //TODO: ugly checking for set types else if (value != null && value.GetType ().IsGenericType && (value.GetType ().GetGenericTypeDefinition () == typeof(Tuple<>) || value.GetType ().GetGenericTypeDefinition () == typeof(Tuple<,>) || value.GetType ().GetGenericTypeDefinition () == typeof(Tuple<,,>) || value.GetType ().GetGenericTypeDefinition () == typeof(Tuple<,,,>) || value.GetType ().GetGenericTypeDefinition () == typeof(Tuple<,,,,>) || value.GetType ().GetGenericTypeDefinition () == typeof(Tuple<,,,,,>))) return EncodeTuple (value, type); //TODO: ugly checking for tuple types else throw new ArgumentException (type + " is not a serializable type"); encoder.Flush (); return ByteString.CopyFrom (stream.ToArray ()); }
internal void <ForBool> b__2_1(CodedOutputStream output, bool value) { output.WriteBool(value); }
/// <summary> /// Convert a Protocol Buffer value type from a C# value to a byte string. /// </summary> public static ByteString WriteValue(object value, Type type) { var stream = new MemoryStream (); var encoder = new CodedOutputStream (stream); if (type == typeof(double)) encoder.WriteDouble ((double)value); else if (type == typeof(float)) encoder.WriteFloat ((float)value); else if (type == typeof(int)) encoder.WriteInt32 ((int)value); else if (type == typeof(long)) encoder.WriteInt64 ((long)value); else if (type == typeof(uint)) encoder.WriteUInt32 ((uint)value); else if (type == typeof(ulong)) encoder.WriteUInt64 ((ulong)value); else if (type == typeof(bool)) encoder.WriteBool ((bool)value); else if (type == typeof(string)) encoder.WriteString ((string)value); else if (type == typeof(byte[])) encoder.WriteBytes (ByteString.CopyFrom ((byte[])value)); else throw new ArgumentException (type + " is not a Protocol Buffer value type"); encoder.Flush (); return ByteString.CopyFrom (stream.ToArray ()); }