I have a very interesting situation, I realized that the Singleton template is not always possible with the .net framework (any version)
look at this code below
namespace SingletonPattern
{
class Singleton
{
private static readonly Singleton instance = new Singleton();
private static int mcount = 0;
private Singleton() {
mcount += 1;
Console.WriteLine("Creating {0} instances of Singleton Class", mcount.ToString());
}
public static Singleton Instance
{
get
{
return instance;
}
}
}
class program
{
static void Main()
{
for (int i = 0; i < 1000; i++)
{
System.Activator.CreateInstance(Type.GetType("SingletonPattern.Singleton"), true);
}
Console.ReadLine();
}
}
}
With System.activator, any buddy can break a singleton pattern.
so who is at risk?
any guy who wrote some licensing component where the license is implemented as a singleton template.
Any server code that uses the Singleton pattern.
Maybe I'm wrong, or my discovery does not make sense, but I just want to share and find out your opinions?
source
share