PageRenderTime 13ms CodeModel.GetById 3ms app.highlight 8ms RepoModel.GetById 1ms app.codeStats 0ms

/Source/Bifrost/Execution/AssemblyLocator.cs

#
C# | 113 lines | 75 code | 13 blank | 25 comment | 2 complexity | dbde582fc67cefbf99425d9d7925fc79 MD5 | raw file
  1#region License
  2//
  3// Copyright (c) 2008-2012, DoLittle Studios and Komplett ASA
  4//
  5// Licensed under the Microsoft Permissive License (Ms-PL), Version 1.1 (the "License")
  6// With one exception :
  7//   Commercial libraries that is based partly or fully on Bifrost and is sold commercially, 
  8//   must obtain a commercial license.
  9//
 10// You may not use this file except in compliance with the License.
 11// You may obtain a copy of the license at 
 12//
 13//   http://bifrost.codeplex.com/license
 14//
 15// Unless required by applicable law or agreed to in writing, software
 16// distributed under the License is distributed on an "AS IS" BASIS,
 17// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 18// See the License for the specific language governing permissions and
 19// limitations under the License.
 20//
 21#endregion
 22using System;
 23#if(!SILVERLIGHT)
 24using System.IO;
 25#else
 26using System.Windows;
 27#endif
 28using System.Linq;
 29using System.Reflection;
 30
 31
 32namespace Bifrost.Execution
 33{
 34	/// <summary>
 35    /// Represents a <see cref="IAssemblyLocator"/>
 36    /// </summary>
 37    [Singleton]
 38    public class AssemblyLocator : IAssemblyLocator
 39    {
 40        Assembly[] _assemblies;
 41
 42        /// <summary>
 43        /// Initializes a new instance of <see cref="AssemblyLocator"/>
 44        /// </summary>
 45        public AssemblyLocator()
 46        {
 47            Initialize();
 48        }
 49
 50        private void Initialize()
 51        {
 52#if(SILVERLIGHT)
 53            _assemblies = (from part in Deployment.Current.Parts
 54                          where ShouldAddAssembly(part.Source)
 55                          let info = Application.GetResourceStream(new Uri(part.Source, UriKind.Relative))
 56                          select part.Load(info.Stream)).ToArray();
 57#else
 58            var codeBase = Assembly.GetExecutingAssembly().CodeBase;
 59            var uri = new Uri(codeBase);
 60            var path = Path.GetDirectoryName(uri.LocalPath);
 61
 62            var files = new DirectoryInfo(path).GetFiles("*.dll");
 63            files.Concat(new DirectoryInfo(path).GetFiles("*.exe"));
 64
 65            var currentAssemblies = AppDomain.CurrentDomain.GetAssemblies().ToList();
 66            foreach (var file in files)
 67            {
 68                var assemblyName = AssemblyName.GetAssemblyName(file.FullName);
 69                if (!currentAssemblies.Any(assembly => AssemblyName.ReferenceMatchesDefinition(assemblyName, assembly.GetName())))
 70                {
 71                    currentAssemblies.Add(Assembly.Load(assemblyName));
 72                }
 73            }
 74            _assemblies = currentAssemblies.Distinct(new AssemblyComparer()).ToArray();
 75#endif
 76        }
 77
 78#pragma warning disable 1591 // Xml Comments
 79        public Assembly[] GetAll()
 80        {
 81            return _assemblies;
 82        }
 83
 84        public Assembly GetWithFullName(string fullName)
 85        {
 86            var query = from a in _assemblies
 87                        where a.FullName == fullName
 88                        select a;
 89
 90            var assembly = query.SingleOrDefault();
 91            return assembly;
 92        }
 93
 94        public Assembly GetWithName(string name)
 95        {
 96            var query = from a in _assemblies
 97                        where a.FullName.Contains(name)
 98                        select a;
 99
100            var assembly = query.SingleOrDefault();
101            return assembly;
102        }
103
104#pragma warning restore 1591 // Xml Comments
105
106#if(SILVERLIGHT)
107		private static bool ShouldAddAssembly(string name)
108		{
109			return !name.Contains("System.");
110		}
111#endif
112    }
113}