我已阅读文档,无法找到有关解析某个类型的任何内容,同时会覆盖其某些依赖项。最容易通过示例来说明
public class A
{
public A(IServiceA a, IServiceB b) {}
}
// Resolve scenarion
type =>
{
// type is A
var a = Container.Resolve<IServiceA>();
a.SomeProperty = "magic";
return Container.Resolve(type) // TODO: How to resolve A using a
}
有意义吗?正在寻找像
这样的东西return Container.Resolve(type, Rule.Override.TypeOf<IServiceA>(a));
DryIoc非常棒
编辑(2016-05-26) 我的问题是误导。以下是完整的代码示例(适用于prism)
ViewModelLocationProvider.SetDefaultViewModelFactory((view, type) =>
{
var page = view as Page;
if (page != null)
{
var navigationService = Container.Resolve<INavigationService>();
((IPageAware)navigationService).Page = page;
var @override = Container.Resolve<Func<INavigationService, type>>(); // How to do this
return @override(navigationService);
}
return Container.Resolve(type);
});
答案 0 :(得分:2)
解析为您要传递的Func with parameter:
var factory = Container.Resolve<Func<IServiceA, A>>();
var result = factory(a);
<强>更新强>
给定要解析的运行时类型:
type =>
{
// type is A
var a = Container.Resolve<IServiceA>();
a.SomeProperty = "magic";
// Asking for required service type, but wrapped in Func returning object
var factory = Container.Resolve<Func<IServiceA, object>>(requiredServiceType: type);
return factory(a);
}