简体   繁体   English

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

[英]How come I can't see member 'Default' on a class derived from ApplicationSettingsBase?

I'm using .NET 3.5 and I have a class, A, marked as internal sealed partial and it derives from System.Configuration.ApplicationSettingsBase. 我正在使用.NET 3.5,并且有一个类A标记为内部密封的部分,它是从System.Configuration.ApplicationSettingsBase派生的。 I then use an instance of this class in the following manner: 然后,我通过以下方式使用此类的实例:

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

Why would the Visual C# compiler be complaining: 为什么Visual C#编译器会抱怨:

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

?

You are probably looking for this: 您可能正在寻找:

private static ServerSettings defaultInstance = ((ServerSettings)(global::System.Configuration.ApplicationSettingsBase.Synchronized(new ServerSettings())));

public static ServerSettings Default 
    {
    get { return defaultInstance; }
}

This is the code that gets generated by visual studio 这是Visual Studio生成的代码

.NET doesn't provide a 'Default' member in ApplicationSettingsBase from which to access any methods. .NET在ApplicationSettingsBase中没有提供“默认”成员,从该成员可以访问任何方法。 What I failed to notice was that 'Default' was being used as a singleton of A. Thus, the sample code provided by gdean2323 gave me some guidance to the fix. 我没有注意到的是'Default'被用作A的单例。因此,gdean2323提供的示例代码为我提供了一些修复指导。 The code to fix the problem required me adding the following code to class A (without necessary synchronization for simplicity): 解决该问题的代码要求我向A类添加以下代码(为简化起见,没有必要进行同步):

private static A defaultInstance = new A();
public static A Default 
{
    get
    {
        return defaultInstance;
    }
}

ApplicationSettingsBase inherits from SettingsBase, neither of which expose a Default property. ApplicationSettingsBase继承自SettingsBase,这两个都不公开Default属性。 Judging by the compiler error your class doesn't add one. 从编译器错误判断,您的类不会添加一个。

AFAIK C# does not support any special syntax around the word 'Default' for accessing a property that is marked as the default. AFAIK C#不支持在“默认”一词周围使用任何特殊语法来访问标记为默认的属性。

What behaviour are you expecting? 您期望什么行为?

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何使用ApplicationSettingsBase将自定义类保存到用户设置? - How do I save a custom class to user settings using ApplicationSettingsBase? 当我创建一个从另一个GameComponent类派生的类时,游戏类只会更新原始类的更新中的内容吗? - How come when I create a class derived from another GameComponent class, the game class will only update what's in the original class' update? 继承-无法访问派生类中的基类数据成员 - Inheritance - Can't access base class data member in derived class 当我有IEnumerable时如何找到一个派生类实现 <T> 来自Ninject绑定 - How can i find one derived class implementation when i have IEnumerable<T> from Ninject binding 如何实现可以从WPF中派生的Singleton类? - How can I implement a Singleton class that can be derived from in WPF? 如何从派生类访问基类的成员变量的值 - how to access the value of the member variables of a base class from a derived class 我如何验证从List派生的类中的添加项 <T> ? - How would I validate additions in a class derived from List<T>? 我怎么知道一个对象是否来自特定的类 - How can I tell if one object is derived from a particular class 从接口继承时如何使用派生类 - How can I use derived class when inherite from an interface 如何从抽象类列表中检索派生类? - How can I retrieve the derived class from a list of abstract classes?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM