1
// -----------------------------------------------------------------------
2 // Copyright (c) Microsoft Corporation. All rights reserved.
3 // -----------------------------------------------------------------------
6 using System
.Collections
.Generic
;
9 using System
.Reflection
;
11 using System
.Windows
.Resources
;
13 using System
.ComponentModel
;
15 namespace System
.ComponentModel
.Composition
.Hosting
18 /// Helper functions for accessing the Silverlight manifest
20 internal static class Package
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.
27 public static IEnumerable
<Assembly
> CurrentAssemblies
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
));
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
);
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();
71 /// Only reads AssemblyParts and does not support external parts (aka Platform Extensions or TPEs).
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
82 // <AssemblyPart x:Name="A" Source="A.dll" />
83 // <AssemblyPart x:Name="B" Source="B.dll" />
85 // <AssemblyPart x:Name="Z" Source="Z.dll" />
86 // </Deployment.Parts>
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");
101 assemblyParts
.Add(new AssemblyPart() { Source = source }
);
104 while (reader
.ReadToNextSibling("AssemblyPart"));
109 return assemblyParts
;