本文转载自:http://www.pin5i.com/showtopic-26068.html
- 1: Assembly LoadAssemblyFromXap(Stream packageStream, String assemblyName)
- 2: {
- 3: StreamResourceInfo resouceInfo = new StreamResourceInfo(packageStream, "application/binary");
- 4: Stream mainfestStream = Application.GetResourceStream(resouceInfo, new Uri("AppManifest.xaml", UriKind.Relative)).Stream;
- 5: String appManifestString = new StreamReader(mainfestStream).ReadToEnd();
- 6:
- 7: XElement deploymentRoot = XDocument.Parse(appManifestString).Root;
- 8: List<XElement> deploymentParts = (from assemblyParts in deploymentRoot.Elements().Elements() select assemblyParts).ToList();
- 9:
- 10: Assembly targetassembly = null;
- 11:
- 12: foreach (XElement xElement in deploymentParts)
- 13: {
- 14: String source = xElement.Attribute("Source").Value;
- 15: if (source == assemblyName)
- 16: {
- 17: StreamResourceInfo streamInfo = Application.GetResourceStream(resouceInfo, new Uri(source, UriKind.Relative));
- 18: AssemblyPart asmPart = new AssemblyPart();
- 19: targetassembly = asmPart.Load(streamInfo.Stream);
- 20: }
- 21: }
- 22: return targetassembly;
- 23: }