Prism WPF (Unity) - 注册执行创建对象的表达式

Prism WPF (Unity) - Register expressions that execute to create objects

有没有办法使用 IContainerRegistry 注册一个方法,这样每次解析对象时都会调用该方法?

例如: builder.Register(c => new ConfigReader("mysection")).As();

对于 Unity,Prism 的容器抽象中有 Register(Type type, Func<object> factoryMethod)。这应该完全符合您的要求:

containerRegistry.Register<IConfigReader>( () => new ConfigReader( "mySection" ) );

不过,这仅适用于 Prism 8。

使用 Prism 7,使用原始容器

containerRegistry.GetContainer().RegisterFactory( typeof(IConfigReader), x => new ConfigReader( "mySection" ) );

使用 Prism 6 及更早版本,甚至更容易,因为容器立即可用,无需挖掘:

Container.RegisterFactory( typeof(IConfigReader), x => new ConfigReader( "mySection" ) );