所以当我尝试使用时:
WebRequest request = WebRequest.Create("http://localhost:9998/API/GetGameById/" + ID.ToString());
WebResponse ws = request.GetResponse();
StreamReader responseStream = new StreamReader(ws.GetResponseStream());
string response = responseStream.ReadToEnd();
Game game;
using (MemoryStream Stream = new MemoryStream(UTF8Encoding.Default.GetBytes(response)))
{
XmlSerializer Serializer = new XmlSerializer(typeof(Game));
game = Serializer.Deserialize(Stream) as Game;
}
responseStream.Close();
return game;
返回的“游戏”与 xmlstring“响应”中的游戏具有不同的属性,就像:
game = Serializer.Deserialize(Stream) as Game;
创建一个游戏对象的新实例,而不是给我指定 ID 的游戏
“字符串响应”锁定如下:
<Game xmlns:i="http://www.w3.org/2001/XMLSchema-instance">
<Gameboard i:nil="true" xmlns:a="http://schemas.microsoft.com/2003/10/Serialization/Arrays"/>
<Id>1</Id>
<Players xmlns:a="http://schemas.microsoft.com/2003/10/Serialization/Arrays">
<a:int>1</a:int>
</Players>
<hsize>0</hsize>
<vsize>0</vsize>
<winner>0</winner>
</Game>
但是返回标记处的游戏会像这样锁定
<Game xmlns:i="http://www.w3.org/2001/XMLSchema-instance">
<Gameboard i:nil="true" xmlns:a="http://schemas.microsoft.com/2003/10/Serialization/Arrays"/>
<Id>0</Id>
<Players i:nil="true" xmlns:a="http://schemas.microsoft.com/2003/10/Serialization/Arrays"/>
<hsize>0</hsize>
<vsize>0</vsize>
<winner>0</winner>
</Game>
游戏课,如果有帮助的话:
[DataContract(Namespace = "")]
[ServiceBehavior(InstanceContextMode = InstanceContextMode.Single)]
public class Game
{
[DataMember]
int Id;
[DataMember]
int[] Players;
[DataMember]
int[] Gameboard;
[DataMember]
int hsize;
[DataMember]
int vsize;
[DataMember]
int winner = 0;
public Game()
{ }
public Game(int newgameID)
{
Id = ++newgameID;
}
public int GetSetId
{
get { return Id; }
set { Id = value; }
}
public int[] GetSetGameboard
{
get { return Gameboard; }
set { Gameboard = value; }
}
public int GetSetwinner
{
get { return winner; }
set { winner = value; }
}
public int[] GetPlayerList
{
get { return Players; }
}
public void AddPlayer()
{
int NewPlayer;
if (Players == null)
Players = new int[0];
List<int> temp = Players.ToList();
if (temp.Count == 0)
NewPlayer = 1;
else
{
NewPlayer = temp.Last();
NewPlayer++;
}
temp.Add(NewPlayer);
Players = temp.ToArray();
}
}
对不起,如果我给了你很多/很少,但这是我的第 3 或第 4 篇文章,所以我还在学习只是问你是否遗漏了什么
非常感谢您抽出宝贵时间帮助我!
祝你有美好的一天!!