简体   繁体   English

在设置类中向下转换-Visual Studio

[英]Downcasting in Settings Class - Visual Studio

I'm using Visual Studio v12.0 and looking at the Settings.Designer.cs file in the Solution Explorer. 我正在使用Visual Studio v12.0,并在解决方案资源管理器中查看Settings.Designer.cs文件。 In the Properties namespace, the Settings derived class is created from ApplicationSettingsBase class like so: 在Properties命名空间中,Settings派生类是从ApplicationSettingsBase类创建的,如下所示:

internal sealed partial class Settings : global::System.Configuration.ApplicationSettingsBase

In the class, this line of code has me confused: 在课堂上,这行代码使我感到困惑:

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

This is an example of downcasting, I believe. 我认为,这是一个垂头丧气的例子。 I'm not clear why this is necessary. 我不清楚为什么这是必要的。 Why not just create an instance of Settings since it's already defined as inheriting the base? 为什么不创建一个Settings实例,因为它已被定义为继承基础?

Here's a longer snippet: 这是一个较长的代码段:

namespace ConfigMgrTest.Properties {

[global::System.Runtime.CompilerServices.CompilerGeneratedAttribute()]
[global::System.CodeDom.Compiler.GeneratedCodeAttribute("Microsoft.VisualStudio.Editors.SettingsDesigner.SettingsSingleFileGenerator", "12.0.0.0")]
internal sealed partial class Settings : global::System.Configuration.ApplicationSettingsBase {

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

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

...rest of the namespace...

} }

the line: 该行:

Settings defaultInstance = ((Settings)(global::System.Configuration.ApplicationSettingsBase.Synchronized(new Settings())));

is equivalent to saying: 相当于说:

Settings s1 = new Settings();
SettingsBase synchronizedBaseSettings = global::System.Configuration.ApplicationSettingsBase.Synchronized(s1);
Settings settings = (Settings)synchronizedBaseSettings;

So the cast is required because ApplicationSettingsBase.Synchronized returns SettingsBase type. 因此,必须进行强制转换,因为ApplicationSettingsBase.Synchronized返回SettingsBase类型。 And ApplicationSettingsBase.Synchronized is called to make the settings object threadsafe. 然后调用ApplicationSettingsBase.Synchronized使设置对象成为线程安全的。 Otherwise you would have to: a) define defaultInstance as SettingsBase or b) do not call ApplicationSettingsBase.Synchronized and risk threading issues. 否则,您将必须:a)将defaultInstance定义为SettingsBase或b)不要调用ApplicationSettingsBase.Synchronized并可能引起线程问题。

I guess nowadays the method ApplicationSettingsBase.Synchronized would be declared as a generic like this: 我想现在,方法ApplicationSettingsBase.Synchronized将被声明为这样的泛型:

public static TSettings Synchronized<TSettings> (TSettings settingsBase) where TSettings: SettingsBase

But this class is probably older than generics in c# ;). 但是此类可能比c#中的泛型要老;)。

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM