有人知道在调用构造函数之前注入属性的IoC容器吗?
我正在开发内部框架,我不想强迫人们从一个具有多个参数的构造函数中派生,使用我的基类(这是一些乏味的事情,比如调度器等等,如果我向基中添加更多内容,那么对于每个使用这个基类的人来说,更新代码都是一项艰巨的任务)。最好是在调用用户构造函数之前设置它们,这样我就不必使用独立的OnInitialised虚拟方法来完成依赖于这些属性的初始化了。
编辑
为了澄清,这里我想要的是属性注入相当于构建链中的字段初始值。
也就是说,在6.5或7.5左右(虽然这会有点奇怪)
发布于 2013-03-07 20:28:36
根据对问题的评论,这个模式似乎满足了我的需要,但是我需要使用TLS来避免服务定位器,在这个例子中(我需要控制以这种方式设置的属性的范围)。
public class TestBase
{
private SchedulerContext schedulerContext = InitScheduler();
public SchedulerContext SchedulerContext
{
get { return this.schedulerContext; }
}
private static SchedulerContext InitScheduler()
{
return ServiceLocator.Current.GetInstance<SchedulerContext>();
}
public TestBase()
{
Console.WriteLine("Is Instantiated: {0}", this.SchedulerContext != null);
}
}
public class TestDerived : TestBase
{
public TestDerived()
{
Console.WriteLine("Is Instantiated: {0}", this.SchedulerContext != null);
}
}这至少避免了对语言对象构造框架进行分解的恐惧:)
https://stackoverflow.com/questions/15280491
复制相似问题