2 // Copyright (c) Microsoft Corporation. All rights reserved.
5 namespace System.Activities.Presentation
7 using System.Activities.Expressions;
8 using System.Activities.Presentation.Hosting;
9 using System.Activities.Presentation.View;
10 using System.Collections.Generic;
11 using System.Collections.ObjectModel;
12 using System.Reflection;
13 using Microsoft.VisualBasic.Activities;
15 internal static class NamespaceHelper
17 private static readonly List<string> BlackListedAssemblies = new List<string>
19 typeof(ViewStateService).Assembly.GetName().FullName,
20 typeof(ViewStateService).Assembly.GetName().Name,
23 internal static IList<string> GetTextExpressionNamespaces(object root, out IList<AssemblyReference> references)
25 if (NamespaceHelper.ShouldUsePropertiesForImplementation(root))
27 references = TextExpression.GetReferencesForImplementation(root);
28 return TextExpression.GetNamespacesForImplementation(root);
32 references = TextExpression.GetReferences(root);
33 return TextExpression.GetNamespaces(root);
37 internal static void SetTextExpressionNamespaces(object root, IList<string> namespaces, IList<AssemblyReference> references)
39 if (NamespaceHelper.ShouldUsePropertiesForImplementation(root))
41 TextExpression.SetNamespacesForImplementation(root, namespaces);
42 TextExpression.SetReferencesForImplementation(root, references);
46 TextExpression.SetNamespaces(root, namespaces);
47 TextExpression.SetReferences(root, references);
51 internal static void SetVisualBasicSettings(object root, VisualBasicSettings settings)
53 if (NamespaceHelper.ShouldUsePropertiesForImplementation(root))
55 VisualBasic.SetSettingsForImplementation(root, settings);
59 VisualBasic.SetSettings(root, settings);
63 internal static void ConvertToTextExpressionImports(VisualBasicSettings settings, out IList<string> importedNamespace, out IList<AssemblyReference> references)
65 importedNamespace = new Collection<string>();
66 List<string> assemblyNames = new List<string>();
67 foreach (VisualBasicImportReference visualbasicImport in settings.ImportReferences)
69 if (!BlackListedAssemblies.Contains(visualbasicImport.Assembly))
71 if (importedNamespace.IndexOf(visualbasicImport.Import) == -1)
73 importedNamespace.Add(visualbasicImport.Import);
76 string displayName = visualbasicImport.Assembly.Split(',')[0];
77 if (assemblyNames.IndexOf(displayName) == -1)
79 assemblyNames.Add(displayName);
84 references = new Collection<AssemblyReference>();
85 foreach (string assemblyName in assemblyNames)
87 AssemblyReference reference = new AssemblyReference
89 AssemblyName = new AssemblyName(assemblyName)
92 references.Add(reference);
96 internal static void ConvertToVBSettings(IList<string> importedNamespaces, IList<AssemblyReference> references, EditingContext context, out VisualBasicSettings settings)
98 Dictionary<string, List<string>> visualBasicImports = new Dictionary<string, List<string>>();
99 foreach (string importedNamespace in importedNamespaces)
101 visualBasicImports.Add(importedNamespace, new List<string>());
104 Collection<Assembly> assemblies = new Collection<Assembly>();
105 IMultiTargetingSupportService multiTargetingService = context.Services.GetService<IMultiTargetingSupportService>();
106 foreach (AssemblyReference reference in references)
109 if (multiTargetingService == null)
111 reference.LoadAssembly();
112 assembly = reference.Assembly;
116 assembly = AssemblyContextControlItem.GetAssembly(reference.AssemblyName, multiTargetingService);
119 if (assembly != null)
121 assemblies.Add(assembly);
125 AssemblyContextControlItem assemblyContextItem = context.Items.GetValue<AssemblyContextControlItem>();
126 AssemblyName localAssembly = null;
127 if (assemblyContextItem != null)
129 localAssembly = assemblyContextItem.LocalAssemblyName;
132 if (localAssembly != null)
134 Assembly assembly = AssemblyContextControlItem.GetAssembly(localAssembly, multiTargetingService);
135 if (assembly != null)
137 assemblies.Add(assembly);
141 foreach (Assembly assembly in assemblies)
143 foreach (Type type in assembly.GetTypes())
145 string ns = type.Namespace;
146 if ((ns != null) && visualBasicImports.ContainsKey(ns))
148 string assemblyName = assembly.GetName().Name;
149 visualBasicImports[ns].Add(assemblyName);
154 settings = new VisualBasicSettings();
155 foreach (KeyValuePair<string, List<string>> entries in visualBasicImports)
157 string importedNamespace = entries.Key;
158 foreach (string assemblyName in entries.Value)
160 settings.ImportReferences.Add(new VisualBasicImportReference
162 Import = importedNamespace,
163 Assembly = assemblyName
169 private static bool ShouldUsePropertiesForImplementation(object root)
171 if ((root is ActivityBuilder) || (root is DynamicActivity))