在 C# 中使用 AppConfig 文件中的值
selenium = new DefaultSelenium(
ConfigurationManager.AppSettings["TestMachine"].ToString(),
4444,
ConfigurationManager.AppSettings["Browser"].ToString(),
ConfigurationManager.AppSettings["URL"].ToString()
);
有没有一种有效的方法来做到这一点,而不是重复:
ConfigurationManager.AppSettings[""].ToString()
selenium = new DefaultSelenium(
ConfigurationManager.AppSettings["TestMachine"].ToString(),
4444,
ConfigurationManager.AppSettings["Browser"].ToString(),
ConfigurationManager.AppSettings["URL"].ToString()
);
Is there an efficient way to do this, instead of repeating:
ConfigurationManager.AppSettings[""].ToString()
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(8)
我认为更好的想法是为所有涉及配置的内容编写一个包装类,尤其是在编写测试时。一个简单的例子可能是:
这种方法将允许您在需要时模拟您的配置并降低复杂性
因此您可以继续:
或者您可以从列表继承您的配置并将输入减少为:
I think a better idea is to write a wrapper class to everything that deals with configuration, especially if you write tests. A simple example might be:
This approach will allow you to mock your configuration when you need it and reduce complexity
So you could proceed with:
or you could inherit your configuration from a List and reduce the typing to:
是的,你可以这样做
,因为它已经是一个字符串。
如果您在多个位置使用 ConfigurationManager.AppSettings["Something"],则应该创建一个静态 Config 类,它通过静态属性读取您的 AppSettings。
Yes, you can do
As it is already a string.
If you're using ConfigurationManager.AppSettings["Something"] at multiple places, you should create a static Config class, that reads your AppSettings via static properties.
您可以转到项目的属性并添加设置,然后使用以下命令读取它:
属性.设置.默认.属性
You can go to the properties of the project and add Settings, then read it with:
Properties.Settings.Default.Property
我总是为每个应用程序创建一个配置类,它包装对 app/web.config 文件的访问并将配置条目公开为属性。例如,这样的:
这有几个优点:
I always create a config class per application which wraps access to the app/web.config file and exposes the config entries as properties. E.g. something like this:
This has several advantages:
如果您想要强类型引用,可以继承
ConfigurationSection
/ConfigurationElementCollection
和ConfigurationElement
。您可以使用
[ConfigurationProperty("key", IsRequired = true, DefaultValue = "*^%)(@")]
和验证器 (如) 为
等。ConfigurationElement
指定默认值>[StringValidator(MinLength = 3)]If you want have strong type reference, you can inherit from
ConfigurationSection
/ConfigurationElementCollection
andConfigurationElement
.You can specify default value to
ConfigurationElement
with[ConfigurationProperty("key", IsRequired = true, DefaultValue = "*^%)(@")]
and validator like[StringValidator(MinLength = 3)]
etc.编写一个辅助函数来获取参数。为了良好的编码实践,您需要首先检查密钥是否确实存在。
Write a helper function to get the parameter. You need to check if the key is actually present in the first place for good coding practices.
从配置文件中提取的唯一“更有效”的方式是使用整个部分,然后迭代您想要的内容。当您最终得到循环代码时,它不太可能比您现在拥有的方法更有效。
用于简化的一种模式是创建“应用程序设置”单例并在应用程序加载时加载。您本质上是创建一个字符串、字符串的通用哈希表(字典等),这样您就可以更轻松地完成查找。但翻阅应用程序设置部分仍然存在开销。
The only "more efficient" manner to pull from a config file is to consume the entire section and then iterate through what you desire. As you end up with looping code, it is unlikely to be more efficient than the method you have now.
One pattern to use to simplify is to create an "App settings" Singleton and load at Application Load. You essentially create a generic hashtable (dictionary, etc.) of string, string, so you can accomplish lookups more easily. But there is still the overhead of ripping through the app settings section.
您可能需要在类名上更有创意,但您可以按照以下方式做一些事情:
You need to be a bit more creative with the class name perhaps but you could do something along the lines of: