挣扎于此代码,这对C#来说还很陌生,在编写此问题之前,我已经尝试过stackoverflow的多个答案,所以请听我说...
var onlinePlayers = new Dictionary<int, Dictionary<string,string>>();
var count = 0;
foreach (BasePlayer player in BasePlayer.activePlayerList)
{
onlinePlayers.Add(
count,
new Dictionary<string, string> {
{ "SteamID", player.userID.ToString() },
{ "PlayerName", player.displayName.ToString() },
{ "IPAddress", Regex.Replace(player.net.connection.ipaddress, @":{1}[0-9]{1}\d*", "").ToString }
}
);
count += 1;
}
foreach (var k in onlinePlayers.Keys)
{
foreach (KeyValuePair<string, string> pair in onlinePlayers[k])
{
Console.WriteLine("{0}: {1}", pair.Key, pair.Value);
}
}
我希望看到
SteamID: 1231231221321
PlayerName: HelloWorld
IPAddress: 127.0.0.1
对于集合中的每个项目。
编译器错误:
编译StatLogger.cs(38,111)时出错:错误CS1503:参数2无法将方法组表达式转换为字符串类型
您()
在末尾缺少括号.ToString()
。
{“ IPAddress”,Regex.Replace(player.net.connection.ipaddress,@“:{1} [0-9] {1} \ d *”,“”).ToString}
更新 :
还 player.net.connection.ipaddress.ToString()
上面的代码会将IP转换为字符串,这应该可以防止错误。
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句