简体   繁体   English

Singleton与Activator.CreateInstance

[英]Singleton with Activator.CreateInstance

I have a class which implements the Singleton design pattern. 我有一个实现Singleton设计模式的类。 However, whenever i try to get an instance of that class, using Activator.CreateInstance(MySingletonType) only the private constructor is called. 但是,每当我尝试使用Activator.CreateInstance(MySingletonType)获取该类的实例时,只会调用私有构造函数。 Is there any way to invoke other method than the private constructor? 除了私有构造函数,还有什么方法可以调用其他方法?

My class is defined as follow: 我的课程定义如下:

public class MySingletonClass{


    private static volatile MySingletonClassinstance;
    private static object syncRoot = new object();
    private MySingletonClass()
            {
                //activator.createInstance() comes here each intantiation.
            }

    public static MySingletonClassInstance
        {
                get
                {
                    if (instance == null)
                    {
                        lock (syncRoot)
                        {
                            if (instance == null)
                                instance = new MySingletonClass();
                        }
                    }
                    return instance;
                }
            }
}

And the instantiation as follow: 实例化如下:

Type assemblyType = Type.GetType(realType + ", " + assemblyName);
IService service = Activator.CreateInstance(assemblyType, true) as IService;

Activator.CreateInstance , except for one extreme edge-case, always creates a new instance. Activator.CreateInstance除一个极端情况外,始终创建一个新实例。 I suggest that you probably dont want to use Activator here. 我建议您可能不想在这里使用Activator

However, if you have no choice, the hacky hack hack hack is to make a class that inherits from ContextBoundObject , and decorate it with a custom subclass of ProxyAttribute . 但是,如果您别无选择,那么hacky hack hack就是创建一个从ContextBoundObject继承的ContextBoundObject ,并使用ProxyAttribute的自定义子类对其进行ProxyAttribute In the custom ProxyAttribute subclass, override CreateInstance to do whatever you want. 在自定义ProxyAttribute子类中,重写CreateInstance以执行所需的任何操作。 This is all kinds of evil. 这是种邪恶。 But it even works with new Foo() . 但它甚至可以与new Foo()

Hei i do not know why are you creating an object of singleton class using reflection. 嘿,我不知道为什么要使用反射创建单例类的对象。

the basic purpose of singleton class is that it has only one object and has global access. 单例类的基本目的是它只有一个对象并且具有全局访问权限。

however you can access any of your method in singleton class like : 但是,您可以访问单例类中的任何方法,例如:

public class MySingletonClass {
    private static volatile MySingletonClass instance;
    private static object syncRoot = new object();
    private  MySingletonClass() { }

    public static MySingletonClass MySingletonClassInstance {
        get {
                if (instance == null) {
                    lock (syncRoot) {
                        if (instance == null)
                            instance = new MySingletonClass();
                    }
                }
            return instance;
        }
    }

    public void CallMySingleTonClassMethod() { }
}

public class program {
    static void Main() {
        //calling a 
        methodMySingletonClass.MySingletonClassInstance
                              .CallMySingleTonClassMethod();      

    }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM