// -----------------------------------------------------------------------
// Copyright (c) Microsoft Corporation. All rights reserved.
// -----------------------------------------------------------------------
#if(SILVERLIGHT)
using System;
using System.Collections.Generic;
using System.IO;
using System.Net;
using System.Reflection;
using System.Windows;
using System.Windows.Resources;
using System.Xml;
using System.ComponentModel;
namespace System.ComponentModel.Composition.Hosting
{
///
/// Helper functions for accessing the Silverlight manifest
///
internal static class Package
{
///
/// Retrieves The current list of assemblies for the application XAP load. Depends on the Deployment.Current property being setup and
/// so can only be accessed after the Application object has be completely constructed.
/// No caching occurs at this level.
///
public static IEnumerable CurrentAssemblies
{
get
{
var assemblies = new List();
// While this may seem like somewhat of a hack, walking the AssemblyParts in the active
// deployment object is the only way to get the list of assemblies loaded by the initial XAP.
foreach (AssemblyPart ap in Deployment.Current.Parts)
{
StreamResourceInfo sri = Application.GetResourceStream(new Uri(ap.Source, UriKind.Relative));
if (sri != null)
{
// Keep in mind that calling Load on an assembly that is already loaded will
// be a no-op and simply return the already loaded assembly object.
Assembly assembly = ap.Load(sri.Stream);
assemblies.Add(assembly);
}
}
return assemblies;
}
}
public static IEnumerable LoadPackagedAssemblies(Stream packageStream)
{
List assemblies = new List();
StreamResourceInfo packageStreamInfo = new StreamResourceInfo(packageStream, null);
IEnumerable parts = GetDeploymentParts(packageStreamInfo);
foreach (AssemblyPart ap in parts)
{
StreamResourceInfo sri = Application.GetResourceStream(
packageStreamInfo, new Uri(ap.Source, UriKind.Relative));
assemblies.Add(ap.Load(sri.Stream));
}
packageStream.Close();
return assemblies;
}
///
/// Only reads AssemblyParts and does not support external parts (aka Platform Extensions or TPEs).
///
private static IEnumerable GetDeploymentParts(StreamResourceInfo xapStreamInfo)
{
Uri manifestUri = new Uri("AppManifest.xaml", UriKind.Relative);
StreamResourceInfo manifestStreamInfo = Application.GetResourceStream(xapStreamInfo, manifestUri);
List assemblyParts = new List();
// The code assumes the following format in AppManifest.xaml
//
//
//
//
// ...
//
//
//
if (manifestStreamInfo != null)
{
Stream manifestStream = manifestStreamInfo.Stream;
using (XmlReader reader = XmlReader.Create(manifestStream))
{
if (reader.ReadToFollowing("AssemblyPart"))
{
do
{
string source = reader.GetAttribute("Source");
if (source != null)
{
assemblyParts.Add(new AssemblyPart() { Source = source });
}
}
while (reader.ReadToNextSibling("AssemblyPart"));
}
}
}
return assemblyParts;
}
}
}
#endif