I've got a class named NetworkDevice within the namespace Classes.Device and a method:
private void DisplayDeviceDetails<T>(T device)
{
throw new NotImplementedException();
}
If I got that right, I may restrict the Type of T like so:
private void DisplayDeviceDetails<T>(T device) where T : Classes.Device.NetworkDevice
{
throw new NotImplementedException();
}
Because I may add other devices in the future I'm wondering if there is a way to expand this behaviour to the namespace Classes.Devices like "where T : Classes.Devices"? Or is creating a Baseclass "Device" the way to go?
I couldn't find anything else besides removing the constraint and checking inside the method itself via
object.OriginElement.GetType().Namespace
Other information was 5+ years old.
Thanks guys!
you can create a base class DeviceBase within the
Classes.Devicesnamespace:Then, modify your method to accept only types derived from
DeviceBase:This way, any class within the Classes.Devices namespace that inherits from DeviceBase can be passed to the DisplayDeviceDetails method.