PageRenderTime 36ms CodeModel.GetById 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
Possible License(s): CC-BY-SA-3.0
  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
  22. using System;
  23. #if(!SILVERLIGHT)
  24. using System.IO;
  25. #else
  26. using System.Windows;
  27. #endif
  28. using System.Linq;
  29. using System.Reflection;
  30. namespace Bifrost.Execution
  31. {
  32. /// <summary>
  33. /// Represents a <see cref="IAssemblyLocator"/>
  34. /// </summary>
  35. [Singleton]
  36. public class AssemblyLocator : IAssemblyLocator
  37. {
  38. Assembly[] _assemblies;
  39. /// <summary>
  40. /// Initializes a new instance of <see cref="AssemblyLocator"/>
  41. /// </summary>
  42. public AssemblyLocator()
  43. {
  44. Initialize();
  45. }
  46. private void Initialize()
  47. {
  48. #if(SILVERLIGHT)
  49. _assemblies = (from part in Deployment.Current.Parts
  50. where ShouldAddAssembly(part.Source)
  51. let info = Application.GetResourceStream(new Uri(part.Source, UriKind.Relative))
  52. select part.Load(info.Stream)).ToArray();
  53. #else
  54. var codeBase = Assembly.GetExecutingAssembly().CodeBase;
  55. var uri = new Uri(codeBase);
  56. var path = Path.GetDirectoryName(uri.LocalPath);
  57. var files = new DirectoryInfo(path).GetFiles("*.dll");
  58. files.Concat(new DirectoryInfo(path).GetFiles("*.exe"));
  59. var currentAssemblies = AppDomain.CurrentDomain.GetAssemblies().ToList();
  60. foreach (var file in files)
  61. {
  62. var assemblyName = AssemblyName.GetAssemblyName(file.FullName);
  63. if (!currentAssemblies.Any(assembly => AssemblyName.ReferenceMatchesDefinition(assemblyName, assembly.GetName())))
  64. {
  65. currentAssemblies.Add(Assembly.Load(assemblyName));
  66. }
  67. }
  68. _assemblies = currentAssemblies.Distinct(new AssemblyComparer()).ToArray();
  69. #endif
  70. }
  71. #pragma warning disable 1591 // Xml Comments
  72. public Assembly[] GetAll()
  73. {
  74. return _assemblies;
  75. }
  76. public Assembly GetWithFullName(string fullName)
  77. {
  78. var query = from a in _assemblies
  79. where a.FullName == fullName
  80. select a;
  81. var assembly = query.SingleOrDefault();
  82. return assembly;
  83. }
  84. public Assembly GetWithName(string name)
  85. {
  86. var query = from a in _assemblies
  87. where a.FullName.Contains(name)
  88. select a;
  89. var assembly = query.SingleOrDefault();
  90. return assembly;
  91. }
  92. #pragma warning restore 1591 // Xml Comments
  93. #if(SILVERLIGHT)
  94. private static bool ShouldAddAssembly(string name)
  95. {
  96. return !name.Contains("System.");
  97. }
  98. #endif
  99. }
  100. }