1 // -----------------------------------------------------------------------
\r
2 // Copyright (c) Microsoft Corporation. All rights reserved.
\r
3 // -----------------------------------------------------------------------
\r
6 using System.Collections.Generic;
\r
9 using System.Reflection;
\r
10 using System.Windows;
\r
11 using System.Windows.Resources;
\r
13 using System.ComponentModel;
\r
15 namespace System.ComponentModel.Composition.Hosting
\r
18 /// Helper functions for accessing the Silverlight manifest
\r
20 internal static class Package
\r
23 /// Retrieves The current list of assemblies for the application XAP load. Depends on the Deployment.Current property being setup and
\r
24 /// so can only be accessed after the Application object has be completely constructed.
\r
25 /// No caching occurs at this level.
\r
27 public static IEnumerable<Assembly> CurrentAssemblies
\r
31 var assemblies = new List<Assembly>();
\r
33 // While this may seem like somewhat of a hack, walking the AssemblyParts in the active
\r
34 // deployment object is the only way to get the list of assemblies loaded by the initial XAP.
\r
35 foreach (AssemblyPart ap in Deployment.Current.Parts)
\r
37 StreamResourceInfo sri = Application.GetResourceStream(new Uri(ap.Source, UriKind.Relative));
\r
40 // Keep in mind that calling Load on an assembly that is already loaded will
\r
41 // be a no-op and simply return the already loaded assembly object.
\r
42 Assembly assembly = ap.Load(sri.Stream);
\r
43 assemblies.Add(assembly);
\r
52 public static IEnumerable<Assembly> LoadPackagedAssemblies(Stream packageStream)
\r
54 List<Assembly> assemblies = new List<Assembly>();
\r
55 StreamResourceInfo packageStreamInfo = new StreamResourceInfo(packageStream, null);
\r
57 IEnumerable<AssemblyPart> parts = GetDeploymentParts(packageStreamInfo);
\r
59 foreach (AssemblyPart ap in parts)
\r
61 StreamResourceInfo sri = Application.GetResourceStream(
\r
62 packageStreamInfo, new Uri(ap.Source, UriKind.Relative));
\r
64 assemblies.Add(ap.Load(sri.Stream));
\r
66 packageStream.Close();
\r
71 /// Only reads AssemblyParts and does not support external parts (aka Platform Extensions or TPEs).
\r
73 private static IEnumerable<AssemblyPart> GetDeploymentParts(StreamResourceInfo xapStreamInfo)
\r
75 Uri manifestUri = new Uri("AppManifest.xaml", UriKind.Relative);
\r
76 StreamResourceInfo manifestStreamInfo = Application.GetResourceStream(xapStreamInfo, manifestUri);
\r
77 List<AssemblyPart> assemblyParts = new List<AssemblyPart>();
\r
79 // The code assumes the following format in AppManifest.xaml
\r
81 // <Deployment.Parts>
\r
82 // <AssemblyPart x:Name="A" Source="A.dll" />
\r
83 // <AssemblyPart x:Name="B" Source="B.dll" />
\r
85 // <AssemblyPart x:Name="Z" Source="Z.dll" />
\r
86 // </Deployment.Parts>
\r
88 if (manifestStreamInfo != null)
\r
90 Stream manifestStream = manifestStreamInfo.Stream;
\r
91 using (XmlReader reader = XmlReader.Create(manifestStream))
\r
93 if (reader.ReadToFollowing("AssemblyPart"))
\r
97 string source = reader.GetAttribute("Source");
\r
101 assemblyParts.Add(new AssemblyPart() { Source = source });
\r
104 while (reader.ReadToNextSibling("AssemblyPart"));
\r
109 return assemblyParts;
\r