Say I have a class and it contructor is public Protocol(Object port) and I would only allow user to pass in the System.IO.Ports.SerialPort or System.Net.Sockets.TcpClient only, other object types will consider error. How can I achieve this enforcement?
Please help!
minminPosted May 30, 2008, 5:38 AM
Jan MontanoPosted May 30, 2008, 5:26 AM
Type x = port.GetType();
if (x == typeof(SerialPort)){
}
else if (x == typeof(TcpClient)){
}
else{
//if this is really what you need. Not good though.
throw new Exception();
}
}
or you could use these to avoid errors:
public Protocol (System.Net.Sockets.TcpClient tcpClient){
}
public Protocol(System.IO.Ports.SerialPort serialPort){
}