本文整理匯總了C#中System.IO.StringReader.GetType方法的典型用法代碼示例。如果您正苦於以下問題:C# StringReader.GetType方法的具體用法?C# StringReader.GetType怎麽用?C# StringReader.GetType使用的例子?那麽, 這裏精選的方法代碼示例或許可以為您提供幫助。您也可以進一步了解該方法所在類System.IO.StringReader
的用法示例。
在下文中一共展示了StringReader.GetType方法的1個代碼示例,這些例子默認根據受歡迎程度排序。您可以為喜歡或者感覺有用的代碼點讚,您的評價將有助於係統推薦出更棒的C#代碼示例。
示例1: Deserialize
public Packet Deserialize(CircularBuffer<byte> buffer)
{
try
{
var bytes = new byte[buffer.Size];
buffer.Peek(bytes, 0, buffer.Size);
var json = Encoding.UTF8.GetString(bytes);
var stringReader = new StringReader(json);
var reader = new JsonTextReader(stringReader);
// 주의
var field =
stringReader.GetType().GetField(
"_pos",
System.Reflection.BindingFlags.NonPublic | System.Reflection.BindingFlags.Instance);
var depth = 0;
var id = 0;
var idField = true;
var hasObject = false;
try
{
while (reader.Read())
{
switch (reader.TokenType)
{
case JsonToken.StartObject:
depth++;
hasObject = true;
break;
case JsonToken.EndObject:
depth--;
if (depth == 0)
break;
break;
case JsonToken.PropertyName:
if (depth == 1 &&
(String)reader.Value == "id")
idField = true;
break;
case JsonToken.Integer:
if (idField)
{
id = Convert.ToInt32(reader.Value);
idField = false;
}
break;
}
}
}
catch (JsonReaderException e)
{
// ignore
}
if (hasObject == false)
return null;
if (depth != 0)
return null;
var count = (int)field.GetValue(stringReader);
buffer.Skip(count);
var slicedJson = json.Substring(0, count);
var packet =
(Packet)JsonConvert.DeserializeObject(
slicedJson, Packet.GetTypeById(id));
packet.packetId = id;
return packet;
}
catch (Exception e)
{
Console.WriteLine(e);
}
return null;
}