我想写一个基类,它实现了惰性静态模式的基本结构。
public class LazyStatic<T>
{
private static T _static;
public static T Static
{
get
{
if (_static == null) _static = Activator.CreateInstance<T>();
return _static;
}
}
}一旦我完成了这个基类,我将像这样使用它
public class MyOtherClass : LazyStatic<MyOtherClass>
{
...
}基类是否正确实现?
发布于 2015-03-19 01:16:41
您假设T有一个无参数的构造函数,但是您没有使用restrict you generic class,以便编译器知道:
public class LazyStatic<T> where T : new()
{
private static T _static;
public static T Static
{
get
{
if (_static == null) _static = new T();
return _static;
}
}
}https://stackoverflow.com/questions/29128441
复制相似问题