Make it a generic class and fix your problem forever. This works because the compiler turns the generic into a new class (something like SigletonManagerOfTypeParamterTypeName
). So the static
variables are not shared amongst instances...
public static class Singleton<TSingletonType>
where TSingletonType: class, new()
{
private static volatile TSingletonType instance;
private static object syncRoot = new Object();
public static TSingletonType Instance
{
get
{
if (instance == null)
{
lock (syncRoot)
{
if (instance == null)
instance = new TSingletonType();
}
}
return instance;
}
}
}
Usage (check it out, it works):
Form frm = Singleton<Form>.Instance;
Control ctrl = Singleton<control>.Instance;
Doing that 'computer thing' ever since the C64.
Sometimes I feel that being a programmer is much like being a doctor: You just have to know everything and if you don't, something dies.
Either being an application or a patient.
Oddly enough, more people care about the death of their application, than the massacre of people...