1 //----------------------------------------------------------------
2 // Copyright (c) Microsoft Corporation. All rights reserved.
3 //----------------------------------------------------------------
4 namespace System.Activities.Presentation.Hosting
7 using System.Collections.Generic;
8 using System.Reflection;
10 using System.Diagnostics.CodeAnalysis;
11 using System.Activities.Presentation.Hosting;
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.
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
25 public AssemblyName LocalAssemblyName
28 [SuppressMessage("Microsoft.Usage", "CA2227:CollectionPropertiesShouldBeReadOnly", Justification = "This is by design")]
29 public IList<AssemblyName> ReferencedAssemblyNames
35 public override Type ItemType
37 get { return typeof(AssemblyContextControlItem); }
40 public IEnumerable<string> AllAssemblyNamesInContext
44 if ((LocalAssemblyName != null) && LocalAssemblyName.CodeBase != null && (File.Exists(new Uri(LocalAssemblyName.CodeBase).LocalPath)))
46 yield return LocalAssemblyName.FullName;
48 foreach (AssemblyName assemblyName in GetEnvironmentAssemblyNames())
50 //avoid returning local name twice
51 if (LocalAssemblyName == null || !assemblyName.FullName.Equals(LocalAssemblyName.FullName, StringComparison.Ordinal))
53 yield return assemblyName.FullName;
60 public IEnumerable<AssemblyName> GetEnvironmentAssemblyNames()
62 if (this.ReferencedAssemblyNames != null)
64 return this.ReferencedAssemblyNames;
68 List<AssemblyName> assemblyNames = new List<AssemblyName>();
69 foreach (Assembly assembly in AppDomain.CurrentDomain.GetAssemblies())
71 if (!assembly.IsDynamic)
73 assemblyNames.Add(assembly.GetName());
80 [SuppressMessage("Microsoft.Naming", "CA1704:IdentifiersShouldBeSpelledCorrectly",
81 Justification = "Multi-Targeting makes sense")]
82 public IEnumerable<Assembly> GetEnvironmentAssemblies (IMultiTargetingSupportService multiTargetingService)
84 if (this.ReferencedAssemblyNames == null)
86 return AppDomain.CurrentDomain.GetAssemblies().Where<Assembly>(assembly => !assembly.IsDynamic);
90 List<Assembly> assemblies = new List<Assembly>();
91 foreach (AssemblyName assemblyName in this.ReferencedAssemblyNames)
93 Assembly assembly = GetAssembly(assemblyName, multiTargetingService);
96 assemblies.Add(assembly);
103 [SuppressMessage("Microsoft.Naming", "CA1704:IdentifiersShouldBeSpelledCorrectly",
104 Justification = "Multi-Targeting makes sense")]
105 public static Assembly GetAssembly(AssemblyName assemblyName, IMultiTargetingSupportService multiTargetingService)
107 Assembly assembly = null;
110 if (multiTargetingService != null)
112 assembly = multiTargetingService.GetReflectionAssembly(assemblyName);
116 assembly = Assembly.Load(assemblyName);
119 catch (FileNotFoundException)
121 //this exception may occur if current project is not compiled yet
123 catch (FileLoadException)
125 //the assembly could not be loaded, ignore the error
127 catch (BadImageFormatException)