我需要一个类来包含 TcpClient 的所有内容 + 几个额外的字段,所以我创建了一个基于 TcpClient 的类:
public class MyClient: TcpClient
{
public string winUser;
public bool needHelp;
public bool needSignOff;
public MyClient() : base() {
this.needHelp = false;
this.needSignOff = false;
this.winUser = "empty";
}
}
现在我需要将所有传入连接创建为此类的对象:
MyClient clientConnection = (MyClient)this.helpServer.AcceptTcpClient();
问题是 AcceptTcpClient() 生成了一个 TcpClient 类对象,当我尝试连接到服务器时,我一直收到这个异常:
listenServer():无法将“System.Net.Sockets.TcpClient”类型的对象转换为类型“Server.MyClient”。
如何将 AcceptTcpClient() 给我的 TcpClient 对象转换为 MyClient 对象?
最佳答案
无法将类型 (TcpClient
) 向下转换为更派生的类型 (MyClient
)。您必须更新 AcceptTcpClient
以返回一个 MyClient
对象。
关于c# - AcceptTcpClient() 和基于 TcpClient 的类,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8251860/