我想获得一个静态类的实例,但如果不在非静态类上实现单例包装器,我似乎无法做到这一点——这可能吗,还是我遗漏了什么?
public class MyInstanceTester
{
public MyInstanceTester()
{
//this is how i get a reference to a singleton now
MyClass instance1 = MyClass.Instance();
//this is what is would like to do (if only the compiler would let me)
MyStaticClass instance2 = MyStaticClass.Instance();
}
}
public class MyClass
{
private static MyClass _myInstance;
static MyClass()
{
_myInstance = new MyClass();
}
public static MyClass Instance()
{
return _myInstance;
}
}
public static class MyStaticClass
{
public static MyStaticClass Instance
{
get
{
return this;
}
}
}