1 //------------------------------------------------------------------------------ 2 // <copyright file="AssemblyCache.cs" company="Microsoft"> 3 // Copyright (c) Microsoft Corporation. All rights reserved. 4 // </copyright> 5 //------------------------------------------------------------------------------ 6 7 namespace System.Web.UI { 8 using System; 9 using System.Collections; 10 using System.Collections.Concurrent; 11 using System.Collections.Generic; 12 using System.Diagnostics; 13 using System.Diagnostics.CodeAnalysis; 14 using System.Reflection; 15 using System.Web.Configuration; 16 using System.Web.Script; 17 18 // Caches Assembly APIs to improve performance 19 internal static class AssemblyCache { 20 // PERF: Cache reference to System.Web.Extensions assembly. Use ScriptManager since it's guaranteed to be in S.W.E 21 public static readonly Assembly SystemWebExtensions = typeof(ScriptManager).Assembly; 22 public static readonly Assembly SystemWeb = typeof(Page).Assembly; 23 24 private static CompilationSection _compilationSection; 25 internal static bool _useCompilationSection = true; 26 27 // Maps string (assembly name) to Assembly 28 private static readonly Hashtable _assemblyCache = Hashtable.Synchronized(new Hashtable()); 29 // Maps assembly to Version 30 // internal so it can be manipulated by the unit test suite 31 internal static readonly Hashtable _versionCache = Hashtable.Synchronized(new Hashtable()); 32 // Maps an assembly to its ajax framework assembly attribute. If it doesn't have one, it maps it to a null value 33 private static readonly ConcurrentDictionary<Assembly, AjaxFrameworkAssemblyAttribute> _ajaxAssemblyAttributeCache = 34 new ConcurrentDictionary<Assembly, AjaxFrameworkAssemblyAttribute>(); 35 36 private static CompilationSection CompilationSection { 37 get { 38 if (_compilationSection == null) { 39 _compilationSection = RuntimeConfig.GetAppConfig().Compilation; 40 } 41 return _compilationSection; 42 } 43 } 44 GetVersion(Assembly assembly)45 public static Version GetVersion(Assembly assembly) { 46 Debug.Assert(assembly != null); 47 Version version = (Version)_versionCache[assembly]; 48 if (version == null) { 49 // use new AssemblyName() instead of assembly.GetName() so it works in medium trust 50 version = new AssemblyName(assembly.FullName).Version; 51 _versionCache[assembly] = version; 52 } 53 return version; 54 } 55 Load(string assemblyName)56 public static Assembly Load(string assemblyName) { 57 Debug.Assert(!String.IsNullOrEmpty(assemblyName)); 58 Assembly assembly = (Assembly)_assemblyCache[assemblyName]; 59 if (assembly == null) { 60 // _useCompilationSection must be set to false in a unit test environment since there 61 // is no http runtime, and therefore no trust level is set. 62 if (_useCompilationSection) { 63 assembly = CompilationSection.LoadAssembly(assemblyName, true); 64 } 65 else { 66 assembly = Assembly.Load(assemblyName); 67 } 68 _assemblyCache[assemblyName] = assembly; 69 } 70 return assembly; 71 } 72 IsAjaxFrameworkAssembly(Assembly assembly)73 public static bool IsAjaxFrameworkAssembly(Assembly assembly) { 74 return (GetAjaxFrameworkAssemblyAttribute(assembly) != null); 75 } 76 GetAjaxFrameworkAssemblyAttribute(Assembly assembly)77 public static AjaxFrameworkAssemblyAttribute GetAjaxFrameworkAssemblyAttribute(Assembly assembly) { 78 Debug.Assert(assembly != null); 79 AjaxFrameworkAssemblyAttribute ajaxFrameworkAssemblyAttribute; 80 if (!_ajaxAssemblyAttributeCache.TryGetValue(assembly, out ajaxFrameworkAssemblyAttribute)) { 81 ajaxFrameworkAssemblyAttribute = SafeGetAjaxFrameworkAssemblyAttribute(assembly); 82 _ajaxAssemblyAttributeCache.TryAdd(assembly, ajaxFrameworkAssemblyAttribute); 83 } 84 return ajaxFrameworkAssemblyAttribute; 85 } 86 87 [SuppressMessage("Microsoft.Design", "CA1031:DoNotCatchGeneralExceptionTypes", Justification = "We do not want to throw from this method.")] SafeGetAjaxFrameworkAssemblyAttribute(ICustomAttributeProvider attributeProvider)88 internal static AjaxFrameworkAssemblyAttribute SafeGetAjaxFrameworkAssemblyAttribute(ICustomAttributeProvider attributeProvider) { 89 try { 90 foreach (Attribute attribute in attributeProvider.GetCustomAttributes(inherit: false)) { 91 AjaxFrameworkAssemblyAttribute ajaxFrameworkAttribute = attribute as AjaxFrameworkAssemblyAttribute; 92 if (ajaxFrameworkAttribute != null) { 93 return ajaxFrameworkAttribute; 94 } 95 } 96 } 97 catch { 98 // Bug 34311: If we are unable to load the attribute, don't throw. 99 } 100 return null; 101 } 102 } 103 } 104