<?xml version="1.0" encoding="utf-8" ?> <configuration> <configSections> <section name="modules" type="Microsoft.Practices.Composite.Modularity.ModulesConfigurationSection, Microsoft.Practices.Composite"/> </configSections> <modules> <module assemblyFile="Modules/ModuleD.dll" moduleType="ModuleD.ModuleD, ModuleD" moduleName="ModuleD"> <dependencies> <dependency moduleName="ModuleB"/> </dependencies> </module> <module assemblyFile="Modules/ModuleB.dll" moduleType="ModuleB.ModuleB, ModuleB" moduleName="ModuleB"/> <module assemblyFile="Modules/ModuleA.dll" moduleType="ModuleA.ModuleA, ModuleA" moduleName="ModuleA"> <dependencies> <dependency moduleName="ModuleD"/> </dependencies> </module> <module assemblyFile="Modules/ModuleC.dll" moduleType="ModuleC.ModuleC, ModuleC" moduleName="ModuleC" startupLoaded="false"/> </modules> </configuration> 这样的话,我只需在GetModuleCatalog方法中直接返回ConfigurationModuleCatalog对象就可以了: protected override IModuleCatalog GetModuleCatalog() { ModuleCatalog catalog = new ConfigurationModuleCatalog(); return catalog; }
罗嗦几句,根据配置文件加载Module的方式远远优于手动编程的方式,虽然配置起来很麻烦,但是在大型项目中是首选。
备注:
module节点:
1、程序集地址
assemblyFile="Modules/ModuleD.dll"
2、程序集的显示名称完全限定
moduleType="ModuleD.ModuleD, ModuleD"
格式:
<完整类型名称>, <程序集显示名称>
说明:
类型的程序集限定名由类型的名称(包括类型的命名空间)加上一个逗号再加上程序集的显示名称组成。
例如,某个类的程序集限定的名称可能类似如下:
TopNamespace.SubNameSpace.ContainingClass+NestedClass, MyAssembly, Version=1.3.0.0, Culture=neutral, PublicKeyToken=b17a5c561934e089
TopNamespace.SubNameSpace.ContainingClass+NestedClass, MyAssembly, Version=1.3.0.0, Culture=neutral, PublicKeyToken=b17a5c561934e089
详细说明:
https://technet.microsoft.com/zh-cn/magazine/system.type.assemblyqualifiedname(VS.90).aspx
3、模块的init类的导出名称
moduleName="ModuleD"
4、根据ModuleType调用模块:
http://stackoverflow.com/questions/8791323/prism-modularity-what-is-moduletype-and-modulename/8791417
protected virtual IModule CreateModule(ModuleInfo moduleInfo) { if (moduleInfo == null) throw new ArgumentNullException("moduleInfo"); //Below Prism is passing the ModuleType property return this.CreateModule(moduleInfo.ModuleType); } protected virtual IModule CreateModule(string typeName) { Type moduleType = Type.GetType(typeName); if (moduleType == null) { throw new ModuleInitializeException( string.Format(CultureInfo.CurrentCulture, Properties.Resources.FailedToGetType, typeName)); } //and then that type is cast to IModule return (IModule)this.serviceLocator.GetInstance(moduleType); }
protected override void ConfigureModuleCatalog() { Type typeA = typeof(ModuleA.ModuleA); ModuleInfo moduleA = new ModuleInfo { // ModuleA没有设置InitializationMode,默认为WhenAvailable ModuleName = typeA.Name, ModuleType = typeA.AssemblyQualifiedName, }; Type typeB = typeof(ModuleB.ModuleB); ModuleInfo moduleB = new ModuleInfo { ModuleName = typeB.Name, ModuleType = typeB.AssemblyQualifiedName, InitializationMode = InitializationMode.OnDemand, }; Type typeC = typeof(ModuleC.ModuleC); ModuleInfo moduleC = new ModuleInfo { ModuleName = typeC.Name, ModuleType = typeC.AssemblyQualifiedName, InitializationMode = InitializationMode.OnDemand, // ModuleC依赖于ModuleB DependsOn = new Collection<string> { moduleB.ModuleName }, }; this.ModuleCatalog.AddModule(moduleA); this.ModuleCatalog.AddModule(moduleB); this.ModuleCatalog.AddModule(moduleC); }
调用:
protected override void ConfigureModuleCatalog() { Type typeA = typeof(ModuleA.ModuleA); ModuleInfo moduleA = new ModuleInfo { // ModuleA没有设置InitializationMode,默认为WhenAvailable ModuleName = typeA.Name, ModuleType = typeA.AssemblyQualifiedName, }; Type typeB = typeof(ModuleB.ModuleB); ModuleInfo moduleB = new ModuleInfo { ModuleName = typeB.Name, ModuleType = typeB.AssemblyQualifiedName, InitializationMode = InitializationMode.OnDemand, }; Type typeC = typeof(ModuleC.ModuleC); ModuleInfo moduleC = new ModuleInfo { ModuleName = typeC.Name, ModuleType = typeC.AssemblyQualifiedName, InitializationMode = InitializationMode.OnDemand, // ModuleC依赖于ModuleB DependsOn = new Collection<string> { moduleB.ModuleName }, }; this.ModuleCatalog.AddModule(moduleA); this.ModuleCatalog.AddModule(moduleB); this.ModuleCatalog.AddModule(moduleC); }