I have the following legacy code:
if (isOnMainWindow
|| (win.GetType() != typeof(winInputBox)
&& win.GetType() != typeof(winMessage)
&& win.GetType() != typeof(winMsgBox)
&& win.GetType() != typeof(winAbout))
ApplicationCommands.Close.Execute(null, win);
This looks horrible. Is there a way to compare multiple types of an objects as in my case?
There is nothing that can do just what you want, but why not use a simpler syntax:
if (!(win is winInputBox) && !(win is winMessage) && !(win is winMsgBox) && !(win is winAbout)) ApplicationCommands.Close.Execute(null, win);
Alternatively, you can use a method to make your life easier:
public static IsNotOneOf(object obj, params Type[] types)
{
foreach (var type in types)
{
if (type.IsAssignableFrom(obj.GetType()) return false;
}
return true;
}
And you can use it as this:
if (IsNotOneOf(win, typeof(winInputBox), typeof(winMessage), typeof(winMsgBox), typeof(winAbout)) ApplicationCommands.Close.Execute(null, win);