これらのクラスがあると仮定しましょう:
public sealed class Singleton
{
private static readonly Singleton instance = new Singleton();
private Controller controller;
private uControl uc; //user control
public static Singleton Instance
{
get { return Singleton.instance; }
}
public Controller GetController
{
get
{
if (this.controller == null)
{
this.controller = new Controller();
}
return this.controller;
}
}
public uControl UC
{
get
{
if (this.uc == null)
{
this.uc = new uControl();
}
return this.uc;
}
}
}
public uControl : UserControl
{
Controller controller = Singleton.Instance.GetController;
// Do some work with controller class here ...
}
public Form1 : Form
{
public Form1()
{
this.Controls.Add(Singleton.Instance.UC);
}
}
public Form2 : Form
{
public Form2()
{
this.Controls.Add(Singleton.Instance.UC);
}
}
シングルトン経由でユーザー コントロール uControl を 2 つの異なるフォームに追加し、同じコントローラー クラスにアクセスしている間、それらが正しく動作することを期待できますか?
これを試すと、最初にインスタンス化されたフォームのみが uControl を正しく表示できます。他のフォームはuControll OKを取得しますが、ユーザーコントロールがあるべきフォームに空のスペースしか表示されません...