[reflection] Coop handles icalls in System.Reflection and System.RuntimeTypeHandle...
[mono.git] / mcs / class / referencesource / System.Activities.Presentation / System.Activities.Presentation / System / Activities / Presentation / NamespaceHelper.cs
1 // <copyright>
2 //   Copyright (c) Microsoft Corporation.  All rights reserved.
3 // </copyright>
4
5 namespace System.Activities.Presentation
6 {
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;
14
15     internal static class NamespaceHelper
16     {
17         private static readonly List<string> BlackListedAssemblies = new List<string>
18         {
19             typeof(ViewStateService).Assembly.GetName().FullName,
20             typeof(ViewStateService).Assembly.GetName().Name,
21         };
22
23         internal static IList<string> GetTextExpressionNamespaces(object root, out IList<AssemblyReference> references)
24         {
25             if (NamespaceHelper.ShouldUsePropertiesForImplementation(root))
26             {
27                 references = TextExpression.GetReferencesForImplementation(root);
28                 return TextExpression.GetNamespacesForImplementation(root);
29             }
30             else
31             {
32                 references = TextExpression.GetReferences(root);
33                 return TextExpression.GetNamespaces(root);
34             }
35         }
36
37         internal static void SetTextExpressionNamespaces(object root, IList<string> namespaces, IList<AssemblyReference> references)
38         {
39             if (NamespaceHelper.ShouldUsePropertiesForImplementation(root))
40             {
41                 TextExpression.SetNamespacesForImplementation(root, namespaces);
42                 TextExpression.SetReferencesForImplementation(root, references);
43             }
44             else
45             {
46                 TextExpression.SetNamespaces(root, namespaces);
47                 TextExpression.SetReferences(root, references);
48             }
49         }
50
51         internal static void SetVisualBasicSettings(object root, VisualBasicSettings settings)
52         {
53             if (NamespaceHelper.ShouldUsePropertiesForImplementation(root))
54             {
55                 VisualBasic.SetSettingsForImplementation(root, settings);                
56             }
57             else
58             {
59                 VisualBasic.SetSettings(root, settings);
60             }
61         }
62
63         internal static void ConvertToTextExpressionImports(VisualBasicSettings settings, out IList<string> importedNamespace, out IList<AssemblyReference> references)
64         {
65             importedNamespace = new Collection<string>();
66             List<string> assemblyNames = new List<string>();
67             foreach (VisualBasicImportReference visualbasicImport in settings.ImportReferences)
68             {
69                 if (!BlackListedAssemblies.Contains(visualbasicImport.Assembly))
70                 {
71                     if (importedNamespace.IndexOf(visualbasicImport.Import) == -1)
72                     {
73                         importedNamespace.Add(visualbasicImport.Import);
74                     }
75
76                     string displayName = visualbasicImport.Assembly.Split(',')[0];
77                     if (assemblyNames.IndexOf(displayName) == -1)
78                     {
79                         assemblyNames.Add(displayName);
80                     }
81                 }
82             }
83
84             references = new Collection<AssemblyReference>();
85             foreach (string assemblyName in assemblyNames)
86             {
87                 AssemblyReference reference = new AssemblyReference
88                 {
89                     AssemblyName = new AssemblyName(assemblyName)
90                 };
91
92                 references.Add(reference);
93             }
94         }
95
96         internal static void ConvertToVBSettings(IList<string> importedNamespaces, IList<AssemblyReference> references, EditingContext context, out VisualBasicSettings settings)
97         {
98             Dictionary<string, List<string>> visualBasicImports = new Dictionary<string, List<string>>();
99             foreach (string importedNamespace in importedNamespaces)
100             {
101                 visualBasicImports.Add(importedNamespace, new List<string>());
102             }
103
104             Collection<Assembly> assemblies = new Collection<Assembly>();
105             IMultiTargetingSupportService multiTargetingService = context.Services.GetService<IMultiTargetingSupportService>();
106             foreach (AssemblyReference reference in references)
107             {
108                 Assembly assembly;
109                 if (multiTargetingService == null)
110                 {
111                     reference.LoadAssembly();
112                     assembly = reference.Assembly;
113                 }
114                 else
115                 {
116                     assembly = AssemblyContextControlItem.GetAssembly(reference.AssemblyName, multiTargetingService);
117                 }
118
119                 if (assembly != null)
120                 {
121                     assemblies.Add(assembly);
122                 }
123             }
124
125             AssemblyContextControlItem assemblyContextItem = context.Items.GetValue<AssemblyContextControlItem>();
126             AssemblyName localAssembly = null;
127             if (assemblyContextItem != null)
128             {
129                 localAssembly = assemblyContextItem.LocalAssemblyName;
130             }
131
132             if (localAssembly != null)
133             {
134                 Assembly assembly = AssemblyContextControlItem.GetAssembly(localAssembly, multiTargetingService);
135                 if (assembly != null)
136                 {
137                     assemblies.Add(assembly);
138                 }
139             }
140
141             foreach (Assembly assembly in assemblies)
142             {
143                 foreach (Type type in assembly.GetTypes())
144                 {
145                     string ns = type.Namespace;
146                     if ((ns != null) && visualBasicImports.ContainsKey(ns))
147                     {
148                         string assemblyName = assembly.GetName().Name;
149                         visualBasicImports[ns].Add(assemblyName);
150                     }
151                 }
152             }
153
154             settings = new VisualBasicSettings();
155             foreach (KeyValuePair<string, List<string>> entries in visualBasicImports)
156             {
157                 string importedNamespace = entries.Key;
158                 foreach (string assemblyName in entries.Value)
159                 {
160                     settings.ImportReferences.Add(new VisualBasicImportReference
161                     {
162                         Import = importedNamespace,
163                         Assembly = assemblyName
164                     });
165                 }
166             }
167         }
168
169         private static bool ShouldUsePropertiesForImplementation(object root)
170         {
171             if ((root is ActivityBuilder) || (root is DynamicActivity))
172             {
173                 return true;
174             }
175             else
176             {
177                 return false;
178             }
179         }
180     }
181 }