/// <summary> /// 获取所有的导出包列表 /// </summary> /// <returns></returns> public IExportedPackage[] GetExportedPackages() { lock (lockObj) { IExportedPackage[] newPackages = new IExportedPackage[packageByDeclarationOrder.Count]; packageByDeclarationOrder.CopyTo(newPackages, 0); return(newPackages); } }
/// <summary> /// 按名称获取导出包列表 /// </summary> /// <param name="name"></param> /// <returns></returns> public IExportedPackage[] GetExportedPackage(string name) { lock (lockObj) { if (packagesByName.Contains(name)) { IList packages = packagesByName[name] as IList; IExportedPackage[] newPackages = new IExportedPackage[packages.Count]; packages.CopyTo(newPackages, 0); return(newPackages); } return(null); } }
/// <summary> /// 解析导出包 /// </summary> /// <param name="bundles"></param> /// <returns></returns> protected virtual void ResolveImportPackage(AbstractBundle bundle) { // 解析依赖包 DependentBundle[] dependencyBundles = bundle.BundleData.DependentBundles; if (dependencyBundles != null && dependencyBundles.Count() > 0) { foreach (var dependency in dependencyBundles) { // 检查导入包包名,如果为空则直接跳过 string symbolicName = dependency.BundleSymbolicName; if (string.IsNullOrEmpty(symbolicName)) { continue; } Version version = dependency.BundleVersion == null ? new Version("1.0.0.0") : dependency.BundleVersion; // 获取插件对象 AbstractBundle dependencyBundle = framework.getBundleBySymbolicName(symbolicName, version); // 如果为空该导入包无效 if ((dependencyBundle == null || dependencyBundle.IsFragment) && dependency.Resolution == ResolutionMode.Mandatory) { throw new BundleException(string.Format("[{0}]Not find dependency bundle({1}_{2}).", bundle.ToString(), symbolicName, version.ToString()), BundleExceptionType.RESOLVE_ERROR); } // 如果未解析或解析失败则导入包无效 if (dependencyBundle.ResolveState == ResolveState.Unresolved || (dependencyBundle.ResolveState == (ResolveState.Resolved | ResolveState.Fault))) { if (dependency.Resolution == ResolutionMode.Mandatory) { throw new BundleException(string.Format("[{0}]Dependency bundle({1}_{2}) failture resolved.", bundle.ToString(), symbolicName, version.ToString()) , BundleExceptionType.RESOLVE_ERROR); } else { continue; } } // 检查是组件依赖还是插件依赖 if (string.IsNullOrEmpty(dependency.AssemblyName)) { ImportBundleImpl importBundle = new ImportBundleImpl(dependencyBundle, dependency.Resolution); bundle.BundleSpecification.AddImportBundle(importBundle); } else { // 获取依赖组件的相关参数 string asmName = dependency.AssemblyName; Version asmVersion = dependency.AssemblyVersion; string culture = dependency.Culture; string publicKeyToken = dependency.PublicKeyToken; // 定义导出包 IExportedPackage exportedPackage = null; // 获取导出包 if (asmVersion == null) { IExportedPackage[] exportedPackages = dependencyBundle.BundleSpecification.GetExportedPackage(asmName); if (exportedPackages == null || exportedPackages.Length == 0) { if (dependency.Resolution == ResolutionMode.Mandatory) { throw new BundleException(string.Format("[{0}]Not find exported package in dependency bundle({1}_{2}) failture resolved.", bundle.ToString(), symbolicName, version.ToString()) , BundleExceptionType.RESOLVE_ERROR); } else { continue; } } exportedPackage = exportedPackages[0]; } else if (!string.IsNullOrEmpty(culture) && !string.IsNullOrEmpty(publicKeyToken)) { exportedPackage = dependencyBundle.BundleSpecification.GetExportedPackage(asmName, version, culture, publicKeyToken); if (exportedPackage == null) { if (dependency.Resolution == ResolutionMode.Mandatory) { throw new BundleException(string.Format("[{0}]Not find exported package in dependency bundle({1}_{2}) failture resolved.", bundle.ToString(), symbolicName, version.ToString()) , BundleExceptionType.RESOLVE_ERROR); } else { continue; } } } // 找到对应导入包,可以建议导入包到导出包的连线 ImportPackageImpl importPackage = new ImportPackageImpl(bundle, dependencyBundle, dependency); bundle.BundleSpecification.AddImportPackage(importPackage); } } } // 扫描片段插件 if (!bundle.IsFragment && bundle.Fragments != null && bundle.Fragments.Length > 0) { foreach (AbstractBundle fragment in bundle.Fragments) { ResolveImportPackage(fragment); } } }