无法定义静态抽象字符串属性

goo*_*oon 14 .net c#

我遇到了一个有趣的问题,我正在寻找一些关于如何最好地处理这个问题的建议......

我有一个抽象类,其中包含一个静态方法,该方法接受我想要定义为抽象属性的静态字符串.问题是C#不支持以下内容(请参阅ConfigurationSectionNameCurrent属性):

    public abstract class ProviderConfiguration : ConfigurationSection
    {
        private const string _defaultProviderPropertyName = "defaultProvider";
        private const string _providersPropertyName = "providers";

        protected static string ConfigurationSectionName { get; }

        public static Configuration Current
        {
            get { return Configuration)ConfigurationManager.GetSection(ConfigurationSectionName); }
        }
    }
Run Code Online (Sandbox Code Playgroud)

我想有一种方法可以解决这个问题,那就是使ConfigurationSectionName不是抽象的,然后在派生类中创建一个新的ConfigurationSectionName定义,但这感觉非常hackish.任何建议都是最受欢迎的.

Gratias!

Meh*_*dad 12

静态成员没有多态性,因此它们不能是抽象的.:(

如果这就是您所需要的,请考虑制作一个Singleton对象,并从该对象中读取该属性.