Singleton template in .Net not possible?

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?

+3
source share
9 answers

, , , "".

+28

, singleton , , - singleton.

+13

, .

.

+11

System.Activator.CreateInstance, . , , , , , .

.

+7

, - , . - , , , .

+4

, private ctor:

private Singleton() { throw new ApplicationException{
     "Don't call System.Activator.CreateInstance on this class"); }

ctor, ... , , , ... :

class Singleton
{    
   private static readonly Singleton inst = new Singleton("MySecretWord");    
   private static int mcount = 0;   
   private Singleton(string secret)
   { if (secret != "MySecretWord")  
       throw new ApplicationException{
           "Don't call Private constructor on this class"); 
   }
   private Singleton() { throw new ApplicationException{
     "Don't call System.Activator.CreateInstance on this class"); }
  public static Singleton Instance { get  {  return inst ;  } }
}

? - , CreateInstance, , ?

+2

.

, , 1, - , Reflection reset . , !

, .. - , , !

" ", ; .

+2

, , - .. , .

..., , , ...

- , .

0

, , Singleton, .

using System;
using System.Diagnostics;

class Program
{
    public static void Main(string[] args)
    {
        //This should work and not throw any exception
        Singleton singleton = Singleton.Instance;
        Debug.Assert(singleton != null);

        //This should throw an exception
        Singleton s = (Singleton)Activator.CreateInstance(typeof(Singleton), true);
    }
}

public class Singleton
{
    private static Singleton uniqueInstance = new Singleton();

    private Singleton() 
    {
        StackTrace trace = new StackTrace();
        StackFrame frame = trace.GetFrame(1);

        //Check that the private constructor is only getting invoked by the static initializer, otherwise throw exception
        if (String.Compare(trace.GetFrame(1).GetMethod().Name, ".cctor") != 0)
            throw new InvalidOperationException(
                "Don't call Private constructor on this class. Abide by Singleton semantics.");
    }

    public static Singleton Instance { get { return uniqueInstance; } }
}
0

Source: https://habr.com/ru/post/1718299/


All Articles