【问题标题】:Error using index property c#使用索引属性 c# 时出错
【发布时间】: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 中编译。
  • 感谢您意识到我的代码甚至无法编译,我想我需要的帮助比我意识到的要多。

标签: c# vb.net oop


【解决方案1】:

要制作与 vb 代码非常相似的东西,您必须执行以下操作。注意 _value 类不是静态的,静态 AppSettings 类必须初始化 _value 类的实例。

public static class AppSettings{
    public static _value Value { get; set; }

    //static setter
    static AppSettings(){
        Value = new _value();
    }
}

public class _value{
    public string this[string nodeName]{
        get{
            //get code here
        }
        set{
            //set code here
        }
    }
}

要使用此代码,您需要执行以下操作。

//get a value
string mySetting = AppSettings.Value["mySetting"];
//set a value
AppSettings.Value["mySetting"] = "myNewSettingValue";

希望这会有所帮助。

【讨论】:

    【解决方案2】:

    我不确定我是否完全理解您的问题。这段代码有帮助吗?

    class Program
    {
        static void Main(string[] args)
        {
            Value v = new Value();
            string output = v["hello"];
            Console.WriteLine(output);
            Console.ReadLine();
        }
    }
    
    public class Value
    {
        public 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");
            }
        }
    }
    

    【讨论】:

      【解决方案3】:

      原始的 VB 代码没有实现索引器,而是实现了“参数化属性”,这在 C# 中不可用。最接近的 C# 等效项是:

      internal static class AppSettings
      {
          public static string get_Value(string NodeName)
          {
              ConfigurationManager.RefreshSection("appSettings");
              return ConfigurationManager.AppSettings[NodeName];
          }
          public static void set_Value(string NodeName, string value)
          {
              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");
          }
      }
      

      【讨论】:

        【解决方案4】:

        C# 不支持静态索引器,因为索引器需要引用 this,而该引用在静态上下文中不存在。您收到该错误的原因是因为 AppSettings.Value 是一种类型,但使用索引器会将其视为数组,这不会奏效。

        您将改为使用 getter 和 setter 函数,即:

        AppSettings.Value.GetSetting("settingName");
        AppSettings.Value.SetSetting("settingName", "settingValue");
        

        或者,您可以使用对象引用来处理索引器。 (有关更多信息,请参阅this page 上的解决方案。)

        有关索引器的更多信息,请参阅this page on MSDN

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 1970-01-01
          • 2010-09-22
          • 2016-11-23
          • 1970-01-01
          • 1970-01-01
          • 2019-04-22
          • 1970-01-01
          • 2010-11-08
          相关资源
          最近更新 更多