public static VisualBasicSettings CollectXmlNamespacesAndAssemblies(ITypeDescriptorContext context) { // access XamlSchemaContext.ReferenceAssemblies // for the Compiled Xaml scenario IList <Assembly> xsCtxReferenceAssemblies = null; IXamlSchemaContextProvider xamlSchemaContextProvider = context.GetService(typeof(IXamlSchemaContextProvider)) as IXamlSchemaContextProvider; if (xamlSchemaContextProvider != null && xamlSchemaContextProvider.SchemaContext != null) { xsCtxReferenceAssemblies = xamlSchemaContextProvider.SchemaContext.ReferenceAssemblies; if (xsCtxReferenceAssemblies != null && xsCtxReferenceAssemblies.Count == 0) { xsCtxReferenceAssemblies = null; } } VisualBasicSettings settings = null; IXamlNamespaceResolver namespaceResolver = (IXamlNamespaceResolver)context.GetService(typeof(IXamlNamespaceResolver)); if (namespaceResolver == null) { return(null); } lock (AssemblyCache.XmlnsMappingsLockObject) { // Fetch xmlnsMappings for the prefixes returned by the namespaceResolver service foreach (NamespaceDeclaration prefix in namespaceResolver.GetNamespacePrefixes()) { ReadOnlyXmlnsMapping mapping; WrapCachedMapping(prefix, out mapping); if (!mapping.IsEmpty) { if (settings == null) { settings = new VisualBasicSettings(); } if (!mapping.IsEmpty) { foreach (ReadOnlyVisualBasicImportReference importReference in mapping.ImportReferences) { if (xsCtxReferenceAssemblies != null) { // this is "compiled Xaml" VisualBasicImportReference newImportReference; if (importReference.EarlyBoundAssembly != null) { if (xsCtxReferenceAssemblies.Contains(importReference.EarlyBoundAssembly)) { newImportReference = importReference.Clone(); newImportReference.EarlyBoundAssembly = importReference.EarlyBoundAssembly; settings.ImportReferences.Add(newImportReference); } continue; } for (int i = 0; i < xsCtxReferenceAssemblies.Count; i++) { AssemblyName xsCtxAssemblyName = VisualBasicHelper.GetFastAssemblyName(xsCtxReferenceAssemblies[i]); if (importReference.AssemblySatisfiesReference(xsCtxAssemblyName)) { // bind this assembly early to the importReference // so later AssemblyName resolution can be skipped newImportReference = importReference.Clone(); newImportReference.EarlyBoundAssembly = xsCtxReferenceAssemblies[i]; settings.ImportReferences.Add(newImportReference); break; } } } else { // this is "loose Xaml" VisualBasicImportReference newImportReference = importReference.Clone(); if (importReference.EarlyBoundAssembly != null) { // VBImportReference.Clone() method deliberately doesn't copy // its EarlyBoundAssembly to the cloned instance. // we need to explicitly copy the original's EarlyBoundAssembly newImportReference.EarlyBoundAssembly = importReference.EarlyBoundAssembly; } settings.ImportReferences.Add(newImportReference); } } } } } } return(settings); }
public static VisualBasicSettings CollectXmlNamespacesAndAssemblies(ITypeDescriptorContext context) { IList <Assembly> referenceAssemblies = null; IXamlSchemaContextProvider service = context.GetService(typeof(IXamlSchemaContextProvider)) as IXamlSchemaContextProvider; if ((service != null) && (service.SchemaContext != null)) { referenceAssemblies = service.SchemaContext.ReferenceAssemblies; if ((referenceAssemblies != null) && (referenceAssemblies.Count == 0)) { referenceAssemblies = null; } } VisualBasicSettings settings = null; IXamlNamespaceResolver resolver = (IXamlNamespaceResolver)context.GetService(typeof(IXamlNamespaceResolver)); if (resolver == null) { return(null); } lock (AssemblyCache.XmlnsMappings) { foreach (System.Xaml.NamespaceDeclaration declaration in resolver.GetNamespacePrefixes()) { XmlnsMapping mapping; XNamespace key = XNamespace.Get(declaration.Namespace); if (!AssemblyCache.XmlnsMappings.TryGetValue(key, out mapping)) { Match match = assemblyQualifiedNamespaceRegex.Match(declaration.Namespace); if (match.Success) { mapping.ImportReferences = new System.Collections.Generic.HashSet <VisualBasicImportReference>(); VisualBasicImportReference item = new VisualBasicImportReference { Assembly = match.Groups["assembly"].Value, Import = match.Groups["namespace"].Value, Xmlns = key }; mapping.ImportReferences.Add(item); } else { mapping.ImportReferences = new System.Collections.Generic.HashSet <VisualBasicImportReference>(); } AssemblyCache.XmlnsMappings[key] = mapping; } if (!mapping.IsEmpty) { if (settings == null) { settings = new VisualBasicSettings(); } foreach (VisualBasicImportReference reference2 in mapping.ImportReferences) { if (referenceAssemblies != null) { VisualBasicImportReference reference3; AssemblyName assemblyName = reference2.AssemblyName; if (reference2.EarlyBoundAssembly != null) { if (referenceAssemblies.Contains(reference2.EarlyBoundAssembly)) { reference3 = reference2.Clone(); reference3.EarlyBoundAssembly = reference2.EarlyBoundAssembly; settings.ImportReferences.Add(reference3); } } else { for (int i = 0; i < referenceAssemblies.Count; i++) { if (AssemblySatisfiesReference(VisualBasicHelper.GetFastAssemblyName(referenceAssemblies[i]), assemblyName)) { reference3 = reference2.Clone(); reference3.EarlyBoundAssembly = referenceAssemblies[i]; settings.ImportReferences.Add(reference3); break; } } } } else { VisualBasicImportReference reference4 = reference2.Clone(); if (reference2.EarlyBoundAssembly != null) { reference4.EarlyBoundAssembly = reference2.EarlyBoundAssembly; } settings.ImportReferences.Add(reference4); } } } } } return(settings); }