我有以下遗留代码:
if (isOnMainWindow
|| (win.GetType() != typeof(winInputBox)
&& win.GetType() != typeof(winMessage)
&& win.GetType() != typeof(winMsgBox)
&& win.GetType() != typeof(winAbout))
ApplicationCommands.Close.Execute(null, win);
这看起来很糟糕。有没有办法像我这样比较多种类型的对象?
最佳答案
或者,如果您可以修改类,则创建一个虚拟接口(interface),例如:
public interface IShouldNotClose
{
}
将界面定义为每个窗口的一部分,然后您的检查就变成了
if (isOnMainWindow
|| !(win is IShouldNotClose))
ApplicationCommands.Close.Execute(null, win);
关于C#比较多种类型,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56081218/