【发布时间】:2016-09-29 20:54:17
【问题描述】:
所以我的主要问题是我在用于访问 AppSettings 的 VB.NET 项目中有一个静态类。原始代码是这样的
Module AppSettings
Public Property Value(ByVal NodeName As String) As String
Get
ConfigurationManager.RefreshSection("appSettings")
Return ConfigurationManager.AppSettings(NodeName)
End Get
Set(ByVal value As String)
Dim Config As Configuration = _
ConfigurationManager.OpenExeConfiguration(Assembly.GetExecutingAssembly().Location)
Config.AppSettings.Settings.Item(NodeName).Value = value
Config.AppSettings.SectionInformation.ForceSave = True
Config.Save(ConfigurationSaveMode.Modified)
ConfigurationManager.RefreshSection("appSettings")
End Set
End Property
End Module
所以经过大量搜索后,我决定使用索引器是可行的方法,我想出了以下 c# 代码,除非我尝试实现它,否则不会导致 *编译错误。当我尝试使用它时,我不知道问题是否与我的实现或语法有关。
public static class AppSettings
{
public static class Value
{
public static string this[string nodeName]
{
get
{
ConfigurationManager.RefreshSection("appSettings");
return ConfigurationManager.AppSettings[nodeName];
}
set
{
Configuration Config = ConfigurationManager.OpenExeConfiguration(Assembly.GetExecutingAssembly().Location);
Config.AppSettings.Settings[nodeName].Value = value;
Config.AppSettings.SectionInformation.ForceSave = true;
Config.Save(ConfigurationSaveMode.Modified);
ConfigurationManager.RefreshSection("appSettings");
}
}
}
}
这是我尝试使用这些新类时显示错误的代码
string mySetting = AppSettings.Value["mySettingName"];
这给了我以下错误
'...AppSettings.Value' is a 'type', which is not valid in the given context
任何帮助将不胜感激。
编辑 在一些有用的 cmets 之后,我意识到代码无法编译,由于某种原因没有向我显示错误。所以我是如何用 C# 实现与工作 VB 代码最相似的方式。
【问题讨论】:
-
不太确定您是如何编译该代码的 - C# 中没有静态索引器 (stackoverflow.com/questions/401232/static-indexers)
-
我可以确认这段代码不能在 VS2015 中编译。
-
感谢您意识到我的代码甚至无法编译,我想我需要的帮助比我意识到的要多。