1 //------------------------------------------------------------------------------
2 // <copyright file="AssemblyCache.cs" company="Microsoft">
3 // Copyright (c) Microsoft Corporation. All rights reserved.
5 //------------------------------------------------------------------------------
7 namespace System.Web.UI {
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;
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;
24 private static CompilationSection _compilationSection;
25 internal static bool _useCompilationSection = true;
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>();
36 private static CompilationSection CompilationSection {
38 if (_compilationSection == null) {
39 _compilationSection = RuntimeConfig.GetAppConfig().Compilation;
41 return _compilationSection;
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;
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);
66 assembly = Assembly.Load(assemblyName);
68 _assemblyCache[assemblyName] = assembly;
73 public static bool IsAjaxFrameworkAssembly(Assembly assembly) {
74 return (GetAjaxFrameworkAssemblyAttribute(assembly) != null);
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);
84 return ajaxFrameworkAssemblyAttribute;
87 [SuppressMessage("Microsoft.Design", "CA1031:DoNotCatchGeneralExceptionTypes", Justification = "We do not want to throw from this method.")]
88 internal static AjaxFrameworkAssemblyAttribute SafeGetAjaxFrameworkAssemblyAttribute(ICustomAttributeProvider attributeProvider) {
90 foreach (Attribute attribute in attributeProvider.GetCustomAttributes(inherit: false)) {
91 AjaxFrameworkAssemblyAttribute ajaxFrameworkAttribute = attribute as AjaxFrameworkAssemblyAttribute;
92 if (ajaxFrameworkAttribute != null) {
93 return ajaxFrameworkAttribute;