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