自动检测应用程序“类型”
我使用我的框架构建了四种不同“类型”的应用程序:
1) Windows 服务 2) 正常应用 3) 服务应用程序(具有 Windows 服务功能但具有本地 GUI 控制台和自动升级能力的普通应用程序) 4) 远程 GUI 控制台
现在我可以通过代码检测应用程序是否是 Windows 服务。但目前为了在其他之间进行检测,我使用需要添加到项目文件中的定义。如果可能的话,我想找到一种不依赖 DEFINES 的替代方法。我最初的想法是使用项目版本信息的“注释”字段。
有什么想法吗?
编辑:我正在寻找一种无论我如何“键入”应用程序都有效的通用技术。目前,我使用项目配置中的 DEFINES,它可以工作,但使代码比使用“if”代码开关稍微混乱,并且因为它存储在 .dproj 文件中,所以可以从视图中隐藏。
解决方案:根据 David 的建议,我最初使用条件定义(以及任何其他信息,例如应用程序是否作为 Windows 服务运行)将所有应用程序映射到 4 种应用程序类型之一,存储在全局可访问的对象。除非链接的文件对于包含在特定应用程序类型中没有意义,否则我用代码替换了几乎所有条件编译标志,这显着提高了代码的可读性。我还实施了一些其他“调整”,但这是基本实施。
I build four different "types" of applications with my framework:
1) Windows Services
2) Normal Applications
3) Service Applications (a normal application with the functionality of a Windows Service but with a local GUI console and an ability to auto-upgrade)
4) Remote GUI Consoles
Now I can detect, through code, if the application is a Windows Service. But currently to detect between the others I use DEFINES that need to be added to the project file. I would like find an alternate way that does not rely on DEFINES if possible. My initial thoughts are to use the Comments field of the project's version info.
Any ideas?
Edit: I am after a general technique that works regardless of how I "type" my applications. At the moment I use DEFINES from the project configuration, which works, but makes the code slightly messier than using "if" code switches, and because it is stored in the .dproj file, can be hidden from view.
Solution: From David's suggestion I initially used the conditional defines (and any other information such as whether the application was running as a Windows Service) to map all applications to one of the 4 application types, stored in a globally accessible object. Unless linking files that made no sense to include with a particular application type, I replaced almost all of my conditional compilation flags with code, which significantly improved the readability of the code. There are a few other "tweaks" I implemented, but that was the basic implementation.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
根据您使用
Application
全局变量的方式,您可以通过检查此全局变量的类型来检测您的应用程序是服务、VCL 还是控制台应用程序。对于控制台应用程序,您可以使用System.IsConsole
多变的。你可以这样使用
Depending how you are using the
Application
global variable you can detect if you application is a Service, a VCL or a console App checking the type of this global variable. for consoles app you can use theSystem.IsConsole
variable.and you can use like this
听起来每个项目都有一个应用程序类型,因此区分 .dpr 文件或 .dproj 文件似乎是合乎逻辑的。
如果是我,我会坚持使用条件条件,但使用将其转换为带有共享辅助方法的 Delphi 枚举的技巧,以使其可读性更好。
It sounds like each project has a single app type so it seems logical to differentiate in either the .dpr file or the .dproj files.
If it was me I'd stick to a conditional but use the trick of converting it into a Delphi enum with a shared helper method to make it read better.