正如您可能知道的那样, “OnCreate”方法用于 ComponentRegistration 类,由 Component.For<> 调用创建。 为什么Classes.FromAssembly缺少这个功能? 为什么我们不能对指定命名空间中的每个类都使用 OnCreate。 有解决办法吗?
我想按如下方式完成:
Classes
.FromThisAssembly()
.InSameNameSpaceAs<TInterface>()
.OnCreate<TInterface>( instance => instance.Method());
最佳答案
Classes
允许您根据约定注册组件,因此它不处理单个组件,而是处理组件集合;您无法使用流畅的界面单独配置它们。但是您可以使用Configure
方法让您可以操作每个组件来满足您的需求。
Classes
.FromThisAssembly()
.InSameNameSpaceAs<TInterface>()
.Configure(c => {
c.OnCreate( instance => instance.Method())
});
还有 ConfigureFor
和 ConfigureIf
方法,可让您过滤要处理的约定中的组件。
关于dependency-injection - 温莎城堡 : OnCreate for BaseOnDescriptor,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26142520/