Dependency injection 如何解析Prism中其他模块中注册的类型?

Dependency injection 如何解析Prism中其他模块中注册的类型?,dependency-injection,unity-container,prism,Dependency Injection,Unity Container,Prism,我正在使用UnityBootTrapper在我的Prism应用程序中注册几个模块 protected override IModuleCatalog GetModuleCatalog() { var catalog = new ModuleCatalog(); catalog .AddModule(typeof(LoginModule)) .AddModule(typeof(AppModule)) .AddModule(typeof(Da

我正在使用UnityBootTrapper在我的Prism应用程序中注册几个模块

protected override IModuleCatalog GetModuleCatalog()
{
    var catalog = new ModuleCatalog();
    catalog
       .AddModule(typeof(LoginModule))
       .AddModule(typeof(AppModule))
       .AddModule(typeof(DataTransformationModule), InitializationMode.OnDemand)
       .AddModule(typeof(SyncModule), InitializationMode.OnDemand);

    return catalog;
}
稍后,我将加载那些设置为响应用户操作动态加载OnDemand的模块。尽管我能够从其他模块加载OnDemand模块,但我在OnDemand加载模块中注册的类型并没有得到解析

public class SyncModule : IModule
{
    private readonly IUnityContainer container;

    public SyncModule(IUnityContainer container)
    {
        this.container = container;
    }

    public void Initialize()
    {
        this.RegisterViewsAndServices();

        ISyncController controller = this.container.Resolve<ISyncController>();
        controller.Run();
    }

    protected void RegisterViewsAndServices()
    {
        this.container.RegisterType<ISyncController, SyncController>();
        this.container.RegisterType<ISyncAnchorsRepository, SyncAnchorsRepository>();
        this.container.RegisterType<ISyncService, SyncService>();
        this.container.RegisterType<IView, SynchronizeView>("SynchronizeView");
        this.container.RegisterType<IView, SyncTrayView>("SyncTrayView");
    }
}
公共类同步模块:IModule
{
专用只读IUnityContainer容器;
公共同步模块(IUnityContainer容器)
{
this.container=容器;
}
公共无效初始化()
{
这是。RegisterViewsAndServices();
ISyncController=this.container.Resolve();
controller.Run();
}
受保护的无效RegisterViewsAndServices()
{
this.container.RegisterType();
this.container.RegisterType();
this.container.RegisterType();
this.container.RegisterType(“SynchronizeView”);
this.container.RegisterType(“SyncTrayView”);
}
}

当我尝试从另一个模块加载在SyncModule(如上所示)中注册的任何类型时,编译器会抛出ResolutionFailedException,因为每个模块都是。是否仍要将同一个IUnityContainer实例注入所有模块?(这会是对Prism的滥用吗?

查看
ResolutionFailedException
的完整信息。尽管有点离谱,但它通常包含失败原因的详细信息。通常,引发此异常的原因不是要解决的类型本身,而是通过构造函数参数注入的依赖项之一。

查看
ResolutionFailedException的完整消息。尽管有点离谱,但它通常包含失败原因的详细信息。通常,引发此异常的原因不是要解决的类型本身,而是通过构造函数参数注入的依赖项之一。

我的错误。我使用的是Type.GetType,而不是程序集的完全限定名来动态加载其他模块中的类型。从模块注册到Unity容器的类型可在其他地方解决。

我的错误。我使用的是Type.GetType,而不是程序集的完全限定名来动态加载其他模块中的类型。从模块注册到Unity容器的类型可在其他地方解析。

我可以解析这些类型,而不会从注册到容器的模块中产生任何问题。我在其他模块中遇到了这个异常。他说你需要查看内部异常,以确保你看到的是真正的错误。或者,您也可以使用ExceptionExtensions.GetRootException(exception)来获取根异常,而不是包含解析错误。我可以解析这些类型,而不会出现注册到容器中的模块的任何问题。我在其他模块中遇到了这个异常。他说你需要查看内部异常,以确保你看到的是真正的错误。或者,您可以使用ExceptionExtensions.GetRootException(exception)来获取根异常,而不是包含解析错误。您应该1)始终向我们显示您的实际代码2)不使用Type.GetType进行任何操作。如果您有需要跨模块使用的类型,请让这些模块共享一个中央库。我通常称之为“合同”集会。如果您正在共享类似ISyncService的内容,只需执行unityContainer.Resolve(),而不是(我认为您正在做的)unityContainer.Resolve(Type.GetType(“SyncModule.ISyncService,SyncModule”);您好,Anderson,您能看看这个问题吗:我已经详细阐述了我在代码示例中使用GetType()的场景。您应该1)始终向我们展示您的真实代码2)不要使用Type.GetType进行任何操作。如果您有需要跨模块使用的类型,请让这些模块共享一个中央库。我通常称之为“合同”集会。如果您正在共享类似ISyncService的内容,只需执行unityContainer.Resolve(),而不是(我认为您正在做的)unityContainer.Resolve(Type.GetType(“SyncModule.ISyncService,SyncModule”);您好,Anderson,您能看看这个问题吗:我已经详细阐述了使用GetType()和代码示例的场景。