本文整理汇总了C#中Google.Protobuf.CodedInputStream.ReadString方法的典型用法代码示例。如果您正苦于以下问题:C# CodedInputStream.ReadString方法的具体用法?C# CodedInputStream.ReadString怎么用?C# CodedInputStream.ReadString使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类Google.Protobuf.CodedInputStream
的用法示例。
在下文中一共展示了CodedInputStream.ReadString方法的5个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C#代码示例。
示例1: Decode
/// <summary>
/// Decode a value of the given type.
/// Should not be called directly. This interface is used by service client stubs.
/// </summary>
public static object Decode(ByteString value, Type type, Connection client)
{
var stream = new CodedInputStream (value.ToByteArray ());
if (type == typeof(double))
return stream.ReadDouble ();
else if (type == typeof(float))
return stream.ReadFloat ();
else if (type == typeof(int))
return stream.ReadInt32 ();
else if (type == typeof(long))
return stream.ReadInt64 ();
else if (type == typeof(uint))
return stream.ReadUInt32 ();
else if (type == typeof(ulong))
return stream.ReadUInt64 ();
else if (type == typeof(bool))
return stream.ReadBool ();
else if (type == typeof(string))
return stream.ReadString ();
else if (type == typeof(byte[]))
return stream.ReadBytes ().ToByteArray ();
else if (type.IsEnum)
return stream.ReadInt32 ();
else if (typeof(RemoteObject).IsAssignableFrom (type)) {
if (client == null)
throw new ArgumentException ("Client not passed when decoding remote object");
var id = stream.ReadUInt64 ();
if (id == 0)
return null;
return (RemoteObject)Activator.CreateInstance (type, client, id);
} else if (typeof(IMessage).IsAssignableFrom (type)) {
IMessage message = (IMessage)Activator.CreateInstance (type);
message.MergeFrom (stream);
return message;
} else if (type.IsGenericType && type.GetGenericTypeDefinition () == typeof(IList<>))
return DecodeList (value, type, client);
else if (type.IsGenericType && type.GetGenericTypeDefinition () == typeof(IDictionary<,>))
return DecodeDictionary (value, type, client);
else if (type.IsGenericType && type.GetGenericTypeDefinition () == typeof(ISet<>))
return DecodeSet (value, type, client);
else if (type.IsGenericType &&
(type.GetGenericTypeDefinition () == typeof(Tuple<>) ||
type.GetGenericTypeDefinition () == typeof(Tuple<,>) ||
type.GetGenericTypeDefinition () == typeof(Tuple<,,>) ||
type.GetGenericTypeDefinition () == typeof(Tuple<,,,>) ||
type.GetGenericTypeDefinition () == typeof(Tuple<,,,,>) ||
type.GetGenericTypeDefinition () == typeof(Tuple<,,,,,>)))
return DecodeTuple (value, type, client); // TODO: ugly handing of tuple types
throw new ArgumentException (type + " is not a serializable type");
}
示例2: SkipGroup_WrongEndGroupTag
public void SkipGroup_WrongEndGroupTag()
{
// Create an output stream with:
// Field 1: string "field 1"
// Start group 2
// Field 3: fixed int32
// End group 4 (should give an error)
var stream = new MemoryStream();
var output = new CodedOutputStream(stream);
output.WriteTag(1, WireFormat.WireType.LengthDelimited);
output.WriteString("field 1");
// The outer group...
output.WriteTag(2, WireFormat.WireType.StartGroup);
output.WriteTag(3, WireFormat.WireType.Fixed32);
output.WriteFixed32(100);
output.WriteTag(4, WireFormat.WireType.EndGroup);
output.Flush();
stream.Position = 0;
// Now act like a generated client
var input = new CodedInputStream(stream);
Assert.AreEqual(WireFormat.MakeTag(1, WireFormat.WireType.LengthDelimited), input.ReadTag());
Assert.AreEqual("field 1", input.ReadString());
Assert.AreEqual(WireFormat.MakeTag(2, WireFormat.WireType.StartGroup), input.ReadTag());
Assert.Throws<InvalidProtocolBufferException>(input.SkipLastField);
}
示例3: SkipGroup
public void SkipGroup()
{
// Create an output stream with a group in:
// Field 1: string "field 1"
// Field 2: group containing:
// Field 1: fixed int32 value 100
// Field 2: string "ignore me"
// Field 3: nested group containing
// Field 1: fixed int64 value 1000
// Field 3: string "field 3"
var stream = new MemoryStream();
var output = new CodedOutputStream(stream);
output.WriteTag(1, WireFormat.WireType.LengthDelimited);
output.WriteString("field 1");
// The outer group...
output.WriteTag(2, WireFormat.WireType.StartGroup);
output.WriteTag(1, WireFormat.WireType.Fixed32);
output.WriteFixed32(100);
output.WriteTag(2, WireFormat.WireType.LengthDelimited);
output.WriteString("ignore me");
// The nested group...
output.WriteTag(3, WireFormat.WireType.StartGroup);
output.WriteTag(1, WireFormat.WireType.Fixed64);
output.WriteFixed64(1000);
// Note: Not sure the field number is relevant for end group...
output.WriteTag(3, WireFormat.WireType.EndGroup);
// End the outer group
output.WriteTag(2, WireFormat.WireType.EndGroup);
output.WriteTag(3, WireFormat.WireType.LengthDelimited);
output.WriteString("field 3");
output.Flush();
stream.Position = 0;
// Now act like a generated client
var input = new CodedInputStream(stream);
Assert.AreEqual(WireFormat.MakeTag(1, WireFormat.WireType.LengthDelimited), input.ReadTag());
Assert.AreEqual("field 1", input.ReadString());
Assert.AreEqual(WireFormat.MakeTag(2, WireFormat.WireType.StartGroup), input.ReadTag());
input.SkipLastField(); // Should consume the whole group, including the nested one.
Assert.AreEqual(WireFormat.MakeTag(3, WireFormat.WireType.LengthDelimited), input.ReadTag());
Assert.AreEqual("field 3", input.ReadString());
}
示例4: ReadInvalidUtf8
public void ReadInvalidUtf8()
{
MemoryStream ms = new MemoryStream();
CodedOutputStream output = new CodedOutputStream(ms);
uint tag = WireFormat.MakeTag(1, WireFormat.WireType.LengthDelimited);
output.WriteRawVarint32(tag);
output.WriteRawVarint32(1);
output.WriteRawBytes(new byte[] {0x80});
output.Flush();
ms.Position = 0;
CodedInputStream input = new CodedInputStream(ms);
Assert.AreEqual(tag, input.ReadTag());
string text = input.ReadString();
Assert.AreEqual('\ufffd', text[0]);
}
示例5: ReadValue
/// <summary>
/// Convert a Protocol Buffer value type, encoded as a byte string, to a C# value.
/// </summary>
public static object ReadValue(ByteString value, Type type)
{
if (value.Length == 0)
throw new ArgumentException ("Value is empty");
var stream = new CodedInputStream (value.ToByteArray ());
if (type == typeof(double)) {
return stream.ReadDouble ();
} else if (type == typeof(float)) {
return stream.ReadFloat ();
} else if (type == typeof(int)) {
return stream.ReadInt32 ();
} else if (type == typeof(long)) {
return stream.ReadInt64 ();
} else if (type == typeof(uint)) {
return stream.ReadUInt32 ();
} else if (type == typeof(ulong)) {
return stream.ReadUInt64 ();
} else if (type == typeof(bool)) {
return stream.ReadBool ();
} else if (type == typeof(string)) {
return stream.ReadString ();
} else if (type == typeof(byte[])) {
return stream.ReadBytes ().ToByteArray();
}
throw new ArgumentException (type + " is not a Protocol Buffer value type");
}