I have this:
public class ClientSession : TcpClient
{
public int SessionGUID = 0;
}
And somewhere in server threads this:
ClientSession client = (ClientSession)tcpListener.AcceptTcpClient(); //cast failure
Ok, i understand that to cast like that, right side object must be instance of ClientSession (that possibly stored in pointer to base class)
but… how to construct ClientSession in that case?
I don’t want to make ClientSession like this:
public class ClientSession
{
public int SessionGUID = 0;
public TcpClient client;
}
TcpListener.AcceptTcpClientwill return aTcpClientobject and nothing else. The type of an object cannot be changed at runtime, so a direct conversion (that preserves object identity and data) is not possible.I think composition (similar to your last code snippet) would be the best approach. You could enable conversion using cast syntax by implementing a custom conversion operator, but i would rather suggest using a constructor that takes a
TcpClienthere:so you could do this: