C# 单例对象的语法

C# 单例对象的语法,c#,watin,C#,Watin,我有以下代码来确保Watin只有一个浏览器实例 public sealed class BrowserIE { static readonly IE _Instance = new IE(); static BrowserIE() { } BrowserIE() { } public static IE Instance { get { return _Instance; } } } 但我不

我有以下代码来确保Watin只有一个浏览器实例

public sealed class BrowserIE
{

    static readonly IE _Instance = new IE();

    static BrowserIE()
    {
    }

    BrowserIE()
    {
    }

    public static IE Instance
    {
        get { return _Instance; }
    }
}
但我不知道在这个类中应用这些设置的位置。我如何/在何处可以将以下设置应用于上述代码,以便它们在之前生效

Settings.Instance.MakeNewIeInstanceVisible=false

我知道我可以在一个有效的方法中使用以下内容,但在上面的示例中,我无法正确使用语法,其中_Instance是静态只读的

Settings.Instance.MakeNewIeInstanceVisible = false;
_Instance = new IE();

为什么不使用静态构造函数呢

public sealed class BrowserIE
{
    static readonly IE _Instance;

    static BrowserIE()
    {
        Settings.Instance.MakeNewIeInstanceVisible = false;
        _Instance = new IE();
    }

    BrowserIE()
    {
    }

    public static IE Instance
    {
        get { return _Instance; }
    }
}

如果您想实现线程保存单例并使用.NET4.0,那么可以使用
System.Lazy

public sealed class BrowserIE
{
    private static readonly Lazy<BrowserIE> _singleInstance = new Lazy<BrowserIE>(() => new BrowserIE());

    private BrowserIE() { }

    public static BrowserIEInstance
    {
        get { return _singleInstance.Value; }
    }
}
public sealed class BrowserIE
{
private static readonly Lazy _singleInstance=new Lazy(()=>new BrowserIE());
私有浏览器(){}
公共静态浏览器安装
{
获取{return\u singleInstance.Value;}
}
}

您可以使用属性初始化:
静态只读IE\u Instance=new IE{MakeNewIeInstanceVisible=false}还研究如何使用
Lazy _Instance=newlazy(()=>newie(){MakeNewIeInstanceVisible=false})
然后使用
\u Instance.Value
访问它,这是
System.Lazy
擅长的事情之一。