简体   繁体   English

C#特定属性

[英]C# Specific Properties

Can I do something like this: 我可以做这样的事情:

Configs.Environment.Development;

I'm currently doing something like this: 我目前正在做这样的事情:

Configs.Environment == "DEV";

I don't particularly care for the strings, but I don't know how to set "specific" properties or if it's possible. 我并不特别在乎字符串,但是我不知道如何设置“特定”属性或是否可能。

Are you talking about enums? 你在说枚举吗?

public enum Environment
{
    Development,
    Test,
    Live
}


Configs.Environment = Environment.Development;

This sounds like the sort of thing that's better handled by a preprocessor directive: 这听起来像是可以通过预处理器指令更好地处理的事情:

#if debug
/* etc */
#elseif production
/* etc */
#endif

您可以通过使Environment成为枚举或使Development成为静态只读字符串来实现。

Yes. 是。

 public static class Configs{
         public static class Environment{
              public static readonly string Development="DEV";
          }

  }

But you probably want ENUMS, and use a factory to set your constants. 但是您可能需要ENUMS,并使用工厂设置常量。

Do you want the setting to take effect at compile time only or at runtime? 您是否希望该设置仅在编译时或在运行时生效? Do you want your user to be able to select different settings after deployment? 您是否希望用户在部署后能够选择其他设置?

If you want a compile time only setting, then a pre-processor directive is what you want. 如果只需要编译时设置,则需要预处理程序指令。

If you want runtime settings, .NET has direct support for .config files and you can access the values in your code (and set them too) via the Settings.Default constructs. 如果需要运行时设置,.NET可以直接支持.config文件,并且可以通过Settings.Default构造访问代码中的值(也可以对其进行设置)

VisualStudio has support for easily creating and maintaining these config files. VisualStudio支持轻松创建和维护这些配置文件。

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM