简体   繁体   English

在.net桌面应用程序中启用或禁用功能的最佳方法是什么?

[英]What's the best method to enable or disable a feature in a .net desktop application

It can be either at compile time or at run-time using a config file. 使用配置文件可以在编译时或在运行时。 Is there a more elegant way than simple (and many) if statements? 有没有比简单(很多)if语句更优雅的方法?

I am targeting especially sets of UI controls that comes for a particular feature. 我的目标是针对特定功能的UI控件集。

Unless your program must squeeze out 100% performance, do it with a config file. 除非您的程序必须挤出100%的性能,否则请使用配置文件进行操作。 It will keep your code cleaner. 它将使您的代码更整洁。

If one option changes many parts of code, don't write many conditionals, write one conditional that picks which class you delegate to. 如果一个选项更改了代码的许多部分,请不要编写许多条件,而要编写一个选择要委派给哪个类的条件。 For instance if a preference picks TCP versus UDP, have your conditional instantiate a TcpProvider or UdpProvider which the rest of your code uses with minimal muss or fuss. 例如,如果首选项选择TCP还是UDP,请让您的条件实例化TcpProvider或UdpProvider,其余的代码使用时要尽量避免混乱。

Compiler directives aren't as flexible, but they are appropriate in some circumstances. 编译器指令不那么灵活,但是在某些情况下它们是适当的。

For instance, by default when you compile in DEBUG mode in VS.NET, there is a 'DEBUG' symbol defined...so you can do 例如,默认情况下,当您在VS.NET中以DEBUG模式进行编译时,定义了“ DEBUG”符号...因此您可以执行

void SomeMethod()
{
    #if DEBUG
    //do something here
    #else
    //do something else
    #endif

}

this will result in only one of those blocks being compiled depending if the DEBUG symbol is defined. 这将导致仅编译这些块之一,具体取决于是否定义了DEBUG符号。

Also, you can define additional symbols in Project Properties -> Build -> Conditional compilation symbols. 另外,您可以在项目属性->生成->条件编译符号中定义其他符号。

Or, via the command line compiler using the /define: switch 或者,通过命令行编译器使用/ define:开关

Perhaps I am assuming too much, but: 也许我承担了太多,但是:

switch (setting) {
  case "development": 
     dostuff;
     break
  case "production":
     dootherstuff;
     break;
  default:
     dothebeststuff;
     break;
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 监视桌面应用程序的最佳方法是什么? - What's the best way to watchdog a desktop application? 从桌面应用程序操作MySQL数据库的最佳方法是什么? - What is the best method of manipulate a MySQL database from a desktop application? 以编程方式启用Windows功能的最佳方法是什么? - What is the best way to enable Windows feature programmatically? 在 .NET 6 中访问配置的最佳方法是什么? - What's the best method to access configuration in .NET 6? 远程控制/调试.NET应用程序的最佳方法是什么? - What is the best method of remotely controlling/debugging a .NET application? .NET Windows窗体应用程序更新自身的最佳方法是什么? - What's the best way for a .NET windows forms application to update itself? 为.Net应用程序实施防篡改的最佳想法是什么 - What's the Best idea to implement an Anti Connection Tampering for .Net application 设置新.NET项目的最佳方法是什么? - What's the best method for setting up a new .NET project? 部署商业桌面.NET应用程序有哪些选择? - What are the options for deploying a commercial desktop .NET application? 在.NET Windows应用程序中存储用户设置的首选方法是什么? - What's the preferred method for storing user settings in a .NET windows application?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM