代码之家  ›  专栏  ›  技术社区  ›  Mike Caron

为什么在从ApplicationSettingsBase派生的类上看不到成员“default”?

  •  0
  • Mike Caron  · 技术社区  · 16 年前

    我使用的是.NET 3.5,我有一个类A,标记为“内部密封部分”,它派生自System.Configuration.ApplicationSettingsBase。然后,我以以下方式使用这个类的一个实例:

    A A_Instance = new A();
    A_Instance.Default.Save();
    

    为什么Visual C编译器会抱怨:

    error CS0117: 'A' does not contain a definition for 'Default'
    

    ?

    3 回复  |  直到 16 年前
        1
  •  3
  •   Greg Dean    16 年前

    您可能正在寻找:

    private static ServerSettings defaultInstance = ((ServerSettings)(global::System.Configuration.ApplicationSettingsBase.Synchronized(new ServerSettings())));
    
    public static ServerSettings Default 
        {
        get { return defaultInstance; }
    }
    

    这是由Visual Studio生成的代码

        2
  •  1
  •   Mike Caron    16 年前

    .NET在ApplicationSettingsBase中不提供“默认”成员,从中可以访问任何方法。我没有注意到“default”被用作a的单例。因此,gdean2323提供的示例代码为修复提供了一些指导。修复该问题的代码要求我将以下代码添加到类A中(为了简单起见,不需要同步):

    private static A defaultInstance = new A();
    public static A Default 
    {
        get
        {
            return defaultInstance;
        }
    }
    
        3
  •  0
  •   Rob Walker    16 年前

    applicationsettingsbase继承自settingsbase,两者都不公开默认属性。根据编译器错误判断,你的类没有添加一个。

    afaik c不支持在单词“default”周围使用任何特殊语法来访问标记为默认值的属性。

    你期待什么样的行为?