请:如果我的问题不明确,请告诉我,我会尝试改写它
1 - 让我们说 的类型= typeof运算(LogOnModel)即可。当 ObjectFactory.GetInstance(t)被调用时,我希望它调用 LogOnModel的参数化构造函数并将其传递给@params
2 - 如果我要向LogOnModel的参数化构造函数添加另一个参数,例如
public LogOnModel(string param, IPageService pageService)
所以ObjectFacytory应该没有任何问题地调用这个构造函数
如何配置/启动ObjectFactory,这样可行吗?
谢谢
EDITED
//Project1/file1.cs
public void LoadModel(Type type, string param)
{
var factory = new ModelFactory();
var model = factory.Get(type, **param**);
}
public class ModelFactory : IModelFactory
{
public PageModel Get(Type t, **string param**)
{
//NOW I NEED SOMEHOW TO PASS **param** TO EVERY INSTANCE THAT INHERITS FROM **PageModel**
return ObjectFactory.GetInstance(t) as PageModel;
}
}
//Project2/file2.cs
public class LogOnModel : PageModel
{
public LogOnModel()
{
}
public LogOnModel(string param)
{
}
}
public class Model2 : PageModel
{
public LogOnModel()
{
}
public LogOnModel(string param)
{
}
}
public class Model3 : PageModel
{
public LogOnModel()
{
}
public LogOnModel(string param)
{
}
}
答案 0 :(得分:1)
StructureMap将默认使用具有最多参数的构造函数,因此该部分很容易。然后你只需要像这样配置param的值:
ObjectFactory.Initialize(i => {
i.For<LogOnModel>().Use<LogOnModel>();
});
调用容器时,使用with方法传入参数:
return ObjectFactory.With("param").EqualTo(param).GetInstance(t) as PageModel;