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!

Jan MontanoRank #1,295 · 1.3K reputationPosted May 30, 2008, 1:26 AM EDT
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){
}
minmin4 reputationPosted May 30, 2008, 1:38 AM EDT