如何解决StackOverflowException
本文关键字:StackOverflowException 解决 何解决 | 更新日期: 2023-09-27 18:10:52
我试着写这个类:
public class ModelManager
{
public OmniacareHomeProductionEntities _db;
public CategoriaManager categoriaManager
{
get { return categoriaManager; }
set
{
if (categoriaManager == null)
{
categoriaManager = new CategoriaManagerImpl();
}
}
}
private static readonly log4net.ILog log = log4net.LogManager.GetLogger(typeof(ModelManager));
public ModelManager()
{
_db = new OmniacareHomeProductionEntities();
}
}
CategoriaManager是一个接口,而CategoriaManagerImpl是一个实现CategoriaManager的类。
我在这种模式下使用ModelManager类:
ModelManager modelManager = new ModelManager();
modelManager.categoriaManager.saveLocalCategory(category, true);
所以当我尝试运行这段代码时,在这一行有一个StackOverflowError
get
{
return categoriaManager;
}
我的错误在哪里?你能帮我吗?
你的问题明显在这里
public CategoriaManager categoriaManager
{
get
{
return categoriaManager;
}
set
{
if (categoriaManager == null)
{
categoriaManager = new CategoriaManagerImpl(); //HERE !!!!!!!!!
}
}
}
成员名与属性名相同,可以这样修改,例如:
public CategoriaManager CatManager //NAME OF HE PROPERTY ISCHANGED !!!!
{
get
{
return categoriaManager;
}
set
{
if (categoriaManager == null)
{
categoriaManager = new CategoriaManagerImpl();
}
}
}
总则:
- 属性使用大写 开头的名称
- 字段使用小写 开头的名称
ModelManager modelManager = new ModelManager();
modelManager.CatManager.saveLocalCategory(category, true);
你应该这样修改:
private CategoriaManager _categoriaManager;
public CategoriaManager CategoriaManager
{
get { return _categoriaManager; }
set
{
if (_categoriaManager == null)
{
_categoriaManager = new CategoriaManagerImpl();
}
}
}