PageRenderTime 17ms CodeModel.GetById 8ms app.highlight 5ms RepoModel.GetById 1ms app.codeStats 0ms

/IronPython_Main/Hosts/Silverlight/Microsoft.Scripting.SilverLight/DynamicAppManifest.cs

#
C# | 106 lines | 56 code | 9 blank | 41 comment | 3 complexity | fcd3c9dfb24b0fa52ad86388f538431e MD5 | raw file
  1/* ****************************************************************************
  2 *
  3 * Copyright (c) Microsoft Corporation. 
  4 *
  5 * This source code is subject to terms and conditions of the Apache License, Version 2.0. A 
  6 * copy of the license can be found in the License.html file at the root of this distribution. If 
  7 * you cannot locate the  Apache License, Version 2.0, please send an email to 
  8 * dlr@microsoft.com. By using this source code in any fashion, you are agreeing to be bound 
  9 * by the terms of the Apache License, Version 2.0.
 10 *
 11 * You must not remove this notice, or any other, from this software.
 12 *
 13 *
 14 * ***************************************************************************/
 15
 16#if CLR2
 17using Microsoft.Scripting.Utils;
 18#endif
 19
 20using System;
 21using System.Collections.Generic;
 22using System.Text;
 23using System.Windows;
 24using System.Reflection;
 25using System.IO;
 26using System.Text.RegularExpressions;
 27
 28namespace Microsoft.Scripting.Silverlight {
 29    public class DynamicAppManifest {
 30
 31#if SILVERLIGHT_3
 32        /// <summary>
 33        /// List of valid assemblies to be inside Microsoft.Scripting.slvx
 34        /// </summary>
 35        private static List<string> _assemblyNames = new List<string>() {
 36            "Microsoft.Scripting.Core",
 37            "Microsoft.Scripting",
 38            "Microsoft.Dynamic",
 39            "Microsoft.Scripting.Silverlight"
 40        };
 41#endif
 42
 43        /// <summary>
 44        /// List of all dynamic language assemblies loaded. Contains assemblies
 45        /// from AssemblyParts after construction. After LoadAssemblies is
 46        /// called, any dynamic language assemblies that were loaded as part of
 47        /// ExternalParts is now part of this list.
 48        /// </summary>
 49        public List<Assembly> Assemblies { get; private set; }
 50
 51        /// <summary>
 52        /// List of all dynamic language extensions found in the AppManifest.
 53        /// </summary>
 54        public List<Uri> Extensions { get; private set; }
 55
 56        /// <summary>
 57        /// "Initializes "Assemblies" with any AssemblyParts, and "Extensions"
 58        /// with any ExtensionParts
 59        /// </summary>
 60        public DynamicAppManifest() {
 61            Assemblies = AssemblyParts();
 62#if SILVERLIGHT_3
 63            Extensions = DLRExtensionParts();
 64#else
 65            Extensions = new List<Uri>();
 66#endif
 67        }
 68
 69        /// <summary>
 70        /// Get all AssemblyParts from the AppManifest, as loaded Assembly instances.
 71        /// </summary>
 72        public List<Assembly> AssemblyParts() {
 73            var result = new List<Assembly>();
 74            foreach (var part in Deployment.Current.Parts) {
 75                try {
 76                    result.Add(XapPAL.PAL.LoadAssembly(Path.GetFileNameWithoutExtension(part.Source)));
 77                } catch (Exception) {
 78                    // skip
 79                    // TODO this shouldn't catch all exceptions ... just any 
 80                    // exceptions that can be thrown the try block ...
 81                }
 82            }
 83            return result;
 84        }
 85
 86#if SILVERLIGHT_3
 87        /// <summary>
 88        /// Get all DLR/language-specific ExtensionParts, which is just the
 89        /// extension containing the Microsoft.* assemblies.
 90        /// </summary>
 91        /// <returns></returns>
 92        private List<Uri> DLRExtensionParts() {
 93            var dlrExtensions = new List<Uri>();
 94            if (Type.GetType("System.Windows.ExtensionPart, System.Windows, Version=2.0.5.0, Culture=neutral, PublicKeyToken=7cec85d7bea7798e") != null) {
 95                foreach (var externalPart in Deployment.Current.ExternalParts) {
 96                    var extensionPart = (ExtensionPart)externalPart;
 97                    if (Regex.IsMatch(extensionPart.Source.ToString(), "Microsoft.Scripting.slvx")) {
 98                        dlrExtensions.Add(extensionPart.Source);
 99                    }
100                }
101            }
102            return dlrExtensions;
103        }
104#endif
105    }
106}