[amd64/tramp] hide interpreter specific trampoline behind ifdef
[mono.git] / mcs / class / referencesource / System.Activities.Presentation / System.Activities.Presentation / System / Activities / Presentation / View / AssemblyContextControlItem.cs
1 //----------------------------------------------------------------
2 // Copyright (c) Microsoft Corporation.  All rights reserved.
3 //----------------------------------------------------------------
4 namespace System.Activities.Presentation.Hosting
5 {
6     using System;
7     using System.Collections.Generic;
8     using System.Reflection;
9     using System.Runtime;
10     using System.Diagnostics.CodeAnalysis;
11     using System.Activities.Presentation.Hosting;
12     using System.IO;
13     using System.Linq;
14
15     //This class is required by the TypeBrowser - it allows browsing defined types either in VS scenario or in
16     //rehosted scenario. The types are divided into two categories - types defined in local assembly (i.e. the one 
17     //contained in current project - for that assembly, types are loaded using GetTypes() method), and all other
18     //referenced types - for them, type list is loaded using GetExportedTypes() method.
19     //
20     //if this object is not set in desinger's Items collection or both members are null, the type 
21     //browser will not display "Browse for types" option.
22     [Fx.Tag.XamlVisible(false)]
23     public sealed class AssemblyContextControlItem : ContextItem
24     {
25         public AssemblyName LocalAssemblyName
26         { get; set; }
27
28         [SuppressMessage("Microsoft.Usage", "CA2227:CollectionPropertiesShouldBeReadOnly", Justification = "This is by design")]
29         public IList<AssemblyName> ReferencedAssemblyNames
30         {
31             get;
32             set;
33         }
34
35         public override Type ItemType
36         {
37             get { return typeof(AssemblyContextControlItem); }
38         }
39
40         public IEnumerable<string> AllAssemblyNamesInContext
41         {
42             get
43             {
44                 if ((LocalAssemblyName != null) && LocalAssemblyName.CodeBase != null && (File.Exists(new Uri(LocalAssemblyName.CodeBase).LocalPath)))
45                 {
46                     yield return LocalAssemblyName.FullName;
47                 }
48                 foreach (AssemblyName assemblyName in GetEnvironmentAssemblyNames())
49                 {
50                     //avoid returning local name twice
51                     if (LocalAssemblyName == null || !assemblyName.FullName.Equals(LocalAssemblyName.FullName, StringComparison.Ordinal))
52                     {
53                         yield return assemblyName.FullName;
54                     }
55                 }
56
57             }
58         }
59
60         public IEnumerable<AssemblyName> GetEnvironmentAssemblyNames()
61         {
62             if (this.ReferencedAssemblyNames != null)
63             {
64                 return this.ReferencedAssemblyNames;
65             }
66             else
67             {
68                 List<AssemblyName> assemblyNames = new List<AssemblyName>();
69                 foreach (Assembly assembly in AppDomain.CurrentDomain.GetAssemblies())
70                 {
71                     if (!assembly.IsDynamic)
72                     {
73                         assemblyNames.Add(assembly.GetName());
74                     }
75                 }
76                 return assemblyNames;
77             }
78         }
79
80         [SuppressMessage("Microsoft.Naming", "CA1704:IdentifiersShouldBeSpelledCorrectly", 
81             Justification = "Multi-Targeting makes sense")]
82         public IEnumerable<Assembly> GetEnvironmentAssemblies (IMultiTargetingSupportService multiTargetingService)
83         {
84             if (this.ReferencedAssemblyNames == null)
85             {
86                 return AppDomain.CurrentDomain.GetAssemblies().Where<Assembly>(assembly => !assembly.IsDynamic);
87             }
88             else
89             {
90                 List<Assembly> assemblies = new List<Assembly>();
91                 foreach (AssemblyName assemblyName in this.ReferencedAssemblyNames)
92                 {
93                     Assembly assembly = GetAssembly(assemblyName, multiTargetingService);
94                     if (assembly != null)
95                     {
96                         assemblies.Add(assembly);
97                     }
98                 }
99                 return assemblies;
100             }
101         }
102
103         [SuppressMessage("Microsoft.Naming", "CA1704:IdentifiersShouldBeSpelledCorrectly",
104             Justification = "Multi-Targeting makes sense")]
105         public static Assembly GetAssembly(AssemblyName assemblyName, IMultiTargetingSupportService multiTargetingService)
106         {
107             Assembly assembly = null;
108             try
109             {
110                 if (multiTargetingService != null)
111                 {
112                     assembly = multiTargetingService.GetReflectionAssembly(assemblyName);
113                 }
114                 else
115                 {
116                     assembly = Assembly.Load(assemblyName);
117                 }
118             }  
119             catch (FileNotFoundException)
120             {
121                 //this exception may occur if current project is not compiled yet
122             }
123             catch (FileLoadException)
124             {
125                 //the assembly could not be loaded, ignore the error
126             }
127             catch (BadImageFormatException)
128             {
129                 //bad assembly
130             }
131             return assembly;
132         }
133     }
134 }