我尝试写这个课:
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;
}
我的错误在哪里?你能救我吗?
答案 0 :(得分:5)
你的问题显然在这里
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);
答案 1 :(得分:3)
你应该改变它:
private CategoriaManager _categoriaManager;
public CategoriaManager CategoriaManager
{
get { return _categoriaManager; }
set
{
if (_categoriaManager == null)
{
_categoriaManager = new CategoriaManagerImpl();
}
}
}