PageRenderTime 24ms CodeModel.GetById 16ms app.highlight 6ms RepoModel.GetById 1ms app.codeStats 0ms

/Mono.Cecil/DefaultAssemblyResolver.cs

http://github.com/jbevain/cecil
C# | 61 lines | 37 code | 15 blank | 9 comment | 4 complexity | df46616763f391ad2e71d6e7b9edab35 MD5 | raw file
 1//
 2// Author:
 3//   Jb Evain (jbevain@gmail.com)
 4//
 5// Copyright (c) 2008 - 2015 Jb Evain
 6// Copyright (c) 2008 - 2011 Novell, Inc.
 7//
 8// Licensed under the MIT/X11 license.
 9//
10
11using System;
12using System.Collections.Generic;
13
14namespace Mono.Cecil {
15
16	public class DefaultAssemblyResolver : BaseAssemblyResolver {
17
18		readonly IDictionary<string, AssemblyDefinition> cache;
19
20		public DefaultAssemblyResolver ()
21		{
22			cache = new Dictionary<string, AssemblyDefinition> (StringComparer.Ordinal);
23		}
24
25		public override AssemblyDefinition Resolve (AssemblyNameReference name)
26		{
27			Mixin.CheckName (name);
28
29			AssemblyDefinition assembly;
30			if (cache.TryGetValue (name.FullName, out assembly))
31				return assembly;
32
33			assembly = base.Resolve (name);
34			cache [name.FullName] = assembly;
35
36			return assembly;
37		}
38
39		protected void RegisterAssembly (AssemblyDefinition assembly)
40		{
41			if (assembly == null)
42				throw new ArgumentNullException ("assembly");
43
44			var name = assembly.Name.FullName;
45			if (cache.ContainsKey (name))
46				return;
47
48			cache [name] = assembly;
49		}
50
51		protected override void Dispose (bool disposing)
52		{
53			foreach (var assembly in cache.Values)
54				assembly.Dispose ();
55
56			cache.Clear ();
57
58			base.Dispose (disposing);
59		}
60	}
61}