[英]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.