چگونه از یک فرم تنها یک عدد بتواند باز شود ؟ (توسط Generic Singleton)
با استفاده از پیاده سازی ژنریک الگوی سینگلتون :
کد:
public class SingletonProvider<T> where T : new()
{
SingletonProvider() { }
public static T Instance
{
get
{
return SingletonCreator.Instance;
}
}
class SingletonCreator
{
static SingletonCreator(){}
private static T instance;
public static T Instance
{
get
{
System.Windows.Forms.Form frm = instance as System.Windows.Forms.Form;
if (instance == null || frm.IsDisposed==true)
instance = new T();
return instance;
}
}
}
}
و برای استفاده :
کد:
BoatForm boat = SingletonProvider<BoatForm>.Instance;