The reason is that it looks like you have two levels in creating your singleton. Your real singleton is actually the SingletonCreator.instance
, and that one never gets cleared.
When Singleton<T>
is creating an instance, it calls on SingletonCreator.instance
to get the actual instance. When you clear, you clear one from Singleton<T>.instance
, but next time you create, it pulls existing one from SingletonCreator.instance
which already has an instance that is returned to you.
I don't think you need the extra layer of SingletonCreator
. Simply move that code to your Singleton<T>
and that'll do it.
public class Singleton<T> where T : class
{
#region Constructors
protected Singleton()
{ }
#endregion Constructors
#region Properties
private static readonly object instanceLock = new object();
private static T instance;
public static T GetInstance()
{
if (instance == null)
{
lock(instanceLock)
{
if (instance == null)
{
instance = typeof(T).InvokeMember(typeof(T).Name,
BindingFlags.CreateInstance | BindingFlags.Instance | BindingFlags.NonPublic, null, null, null, CultureInfo.CurrentCulture) as T;
}
}
}
return instance;
}
public static void ClearInstance()
{ instance = null; }
#endregion Properties
}