// // Copyright (c) Microsoft Corporation. All rights reserved. // namespace System.Activities.Presentation { using System.Activities.Expressions; using System.Activities.Presentation.Hosting; using System.Activities.Presentation.View; using System.Collections.Generic; using System.Collections.ObjectModel; using System.Reflection; using Microsoft.VisualBasic.Activities; internal static class NamespaceHelper { private static readonly List BlackListedAssemblies = new List { typeof(ViewStateService).Assembly.GetName().FullName, typeof(ViewStateService).Assembly.GetName().Name, }; internal static IList GetTextExpressionNamespaces(object root, out IList references) { if (NamespaceHelper.ShouldUsePropertiesForImplementation(root)) { references = TextExpression.GetReferencesForImplementation(root); return TextExpression.GetNamespacesForImplementation(root); } else { references = TextExpression.GetReferences(root); return TextExpression.GetNamespaces(root); } } internal static void SetTextExpressionNamespaces(object root, IList namespaces, IList references) { if (NamespaceHelper.ShouldUsePropertiesForImplementation(root)) { TextExpression.SetNamespacesForImplementation(root, namespaces); TextExpression.SetReferencesForImplementation(root, references); } else { TextExpression.SetNamespaces(root, namespaces); TextExpression.SetReferences(root, references); } } internal static void SetVisualBasicSettings(object root, VisualBasicSettings settings) { if (NamespaceHelper.ShouldUsePropertiesForImplementation(root)) { VisualBasic.SetSettingsForImplementation(root, settings); } else { VisualBasic.SetSettings(root, settings); } } internal static void ConvertToTextExpressionImports(VisualBasicSettings settings, out IList importedNamespace, out IList references) { importedNamespace = new Collection(); List assemblyNames = new List(); foreach (VisualBasicImportReference visualbasicImport in settings.ImportReferences) { if (!BlackListedAssemblies.Contains(visualbasicImport.Assembly)) { if (importedNamespace.IndexOf(visualbasicImport.Import) == -1) { importedNamespace.Add(visualbasicImport.Import); } string displayName = visualbasicImport.Assembly.Split(',')[0]; if (assemblyNames.IndexOf(displayName) == -1) { assemblyNames.Add(displayName); } } } references = new Collection(); foreach (string assemblyName in assemblyNames) { AssemblyReference reference = new AssemblyReference { AssemblyName = new AssemblyName(assemblyName) }; references.Add(reference); } } internal static void ConvertToVBSettings(IList importedNamespaces, IList references, EditingContext context, out VisualBasicSettings settings) { Dictionary> visualBasicImports = new Dictionary>(); foreach (string importedNamespace in importedNamespaces) { visualBasicImports.Add(importedNamespace, new List()); } Collection assemblies = new Collection(); IMultiTargetingSupportService multiTargetingService = context.Services.GetService(); foreach (AssemblyReference reference in references) { Assembly assembly; if (multiTargetingService == null) { reference.LoadAssembly(); assembly = reference.Assembly; } else { assembly = AssemblyContextControlItem.GetAssembly(reference.AssemblyName, multiTargetingService); } if (assembly != null) { assemblies.Add(assembly); } } AssemblyContextControlItem assemblyContextItem = context.Items.GetValue(); AssemblyName localAssembly = null; if (assemblyContextItem != null) { localAssembly = assemblyContextItem.LocalAssemblyName; } if (localAssembly != null) { Assembly assembly = AssemblyContextControlItem.GetAssembly(localAssembly, multiTargetingService); if (assembly != null) { assemblies.Add(assembly); } } foreach (Assembly assembly in assemblies) { foreach (Type type in assembly.GetTypes()) { string ns = type.Namespace; if ((ns != null) && visualBasicImports.ContainsKey(ns)) { string assemblyName = assembly.GetName().Name; visualBasicImports[ns].Add(assemblyName); } } } settings = new VisualBasicSettings(); foreach (KeyValuePair> entries in visualBasicImports) { string importedNamespace = entries.Key; foreach (string assemblyName in entries.Value) { settings.ImportReferences.Add(new VisualBasicImportReference { Import = importedNamespace, Assembly = assemblyName }); } } } private static bool ShouldUsePropertiesForImplementation(object root) { if ((root is ActivityBuilder) || (root is DynamicActivity)) { return true; } else { return false; } } } }