为什么C#中不允许使用静态索引器?我认为没有理由不允许它们,而且它们可能非常有用.

例如:

public static class ConfigurationManager 
{
        public object this[string name]
        {
            get => ConfigurationManager.getProperty(name);
            set => ConfigurationManager.editProperty(name, value);
        }

        /// <summary>
        /// This will write the value to the property. Will overwrite if the property is already there
        /// </summary>
        /// <param name="name">Name of the property</param>
        /// <param name="value">Value to be wrote (calls ToString)</param>
        public static void editProperty(string name, object value) 
        {
            var ds = new DataSet();
            var configFile = new FileStream("./config.xml", FileMode.OpenOrCreate);
            ds.ReadXml(configFile);

            if (ds.Tables["config"] == null)
                ds.Tables.Add("config");

            var config = ds.Tables["config"];

            if (config.Rows[0] == null) 
                config.Rows.Add(config.NewRow());

            if (config.Columns[name] == null) 
                config.Columns.Add(name);

            config.Rows[0][name] = value.ToString();

            ds.WriteXml(configFile);
            configFile.Close();
        }

        public static void addProperty(string name, object value) =>
            ConfigurationManager.editProperty(name, value);

        public static object getProperty(string name) 
        {
            var ds = new DataSet();
            var configFile = new FileStream("./config.xml", FileMode.OpenOrCreate);
            ds.ReadXml(configFile);
            configFile.Close();

            if (ds.Tables["config"] == null) return null;

            var config = ds.Tables["config"];

            if (config.Rows[0] == null) return null;
            if (config.Columns[name] == null) return null;

            return config.Rows[0][name];
        }
    }

上述代码将从静态索引器中受益匪浅.但是它不会编译,因为不允许使用静态索引器.为什么会这样?

推荐答案

索引器表示法需要引用this.因为静电方法没有对类的任何特定实例的引用,所以不能对它们使用this,因此不能在静电方法上使用索引器表示法.

您的问题的解决方案是使用单例模式,如下所示:

public class Utilities
{
    private static ConfigurationManager _configurationManager = new ConfigurationManager();
    public static ConfigurationManager ConfigurationManager => _configurationManager;
}

public class ConfigurationManager
{
    public object this[string value]
    {
        get => new object();
        set => // set something
    }
}

现在您可以使用索引器表示法调用Utilities.ConfigurationManager["someKey"].

.net相关问答推荐

Blazor服务器应用程序需要在页面上点击才能与元素交互

.NET 4.5 项目未在 Visual Studio 2022 中编译

在 C# 中获取 log4net 日志(log)文件

找不到 Microsoft.Office.Interop Visual Studio

如何在 WPF 应用程序中使用 App.config 文件?

托管和非托管代码、内存和大小有什么区别?

.NET 中工作线程和 I/O 线程的简单描述

注册 COM 互操作与使程序集 COM 可见

从 Web.Config 中的邮箱友好显示名称存储 Smtp

.NET - 实现捕获所有异常处理程序的最佳方法是什么

你如何调试 MVC 4 API 路由?

如何在不丢失数据的情况下重命名 Entity Framework 5 Code First 迁移中的数据库列?

如何将 XPath 与 XElement 或 LINQ 一起使用?

更改 SqlConnection 超时

铸造:(NewType)与对象作为NewType

使用没有catch块的try-finally块

可以从 C# 调用 C++ 代码吗?

有没有一种简单的方法来判断 .NET Framework 版本?

我可以为 Dictionary 条目使用集合初始化程序吗?

检测到包降级警告(dotnet core,vs 2017)