1

我已阅读文档,但找不到任何关于解析类型并同时覆盖其某些依赖项的信息。举个例子最容易说明

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);
});
4

1 回答 1

2

使用您要传递的参数解析为Func :

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);
}
于 2016-05-25T20:19:19.337 回答