问题描述
我一直在网上搜索如何使用在运行时使用参数的 Prism DryIOC 解析实例,但幸运的是。
internal sealed class ItemInfoHelper : IItemInfoHelper
{
//ctor
public ItemInfoHelper(Item item) {...}
public string GetSomething() {...}
}
protected override void RegisterTypes(IContainerRegistry containerRegistry)
{
containerRegistry.Register<IItemInfoHelper,ItemInfoHelper>();
}
如果我这样做,当我在某处解决它时:
var helperInstance = container.Resolve<IItemInfoHelper>();
显然会使用空 Item(使用默认 Item 构造函数)来解决。我见过很多使用编译时已知的参数注册实例的例子。但情况是,我想解决使用 Item 动态初始化的实例,该实例在不同的地方是不同的(即仅在运行时已知)。
如果我使用 Prism + DryIoc,有没有办法使用这种行为来注册/解决它? 提前致谢。
解决方法
最简单的类型安全方法是解析 item 的函数:
var getHelperInstance = container.Resolve<Func<Item,IItemInfoHelper>>();
var helperInstance = getHelperInstance(myItem);