1 | // Copyright (c) 2010-2013 AlphaSierraPapa for the SharpDevelop Team |
---|
2 | // |
---|
3 | // Permission is hereby granted, free of charge, to any person obtaining a copy of this |
---|
4 | // software and associated documentation files (the "Software"), to deal in the Software |
---|
5 | // without restriction, including without limitation the rights to use, copy, modify, merge, |
---|
6 | // publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons |
---|
7 | // to whom the Software is furnished to do so, subject to the following conditions: |
---|
8 | // |
---|
9 | // The above copyright notice and this permission notice shall be included in all copies or |
---|
10 | // substantial portions of the Software. |
---|
11 | // |
---|
12 | // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, |
---|
13 | // INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR |
---|
14 | // PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE |
---|
15 | // FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR |
---|
16 | // OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER |
---|
17 | // DEALINGS IN THE SOFTWARE. |
---|
18 | |
---|
19 | using System; |
---|
20 | |
---|
21 | namespace ICSharpCode.NRefactory.TypeSystem.Implementation |
---|
22 | { |
---|
23 | /// <summary> |
---|
24 | /// References an existing assembly by name. |
---|
25 | /// </summary> |
---|
26 | [Serializable] |
---|
27 | public sealed class DefaultAssemblyReference : IAssemblyReference, ISupportsInterning |
---|
28 | { |
---|
29 | public static readonly IAssemblyReference CurrentAssembly = new CurrentAssemblyReference(); |
---|
30 | |
---|
31 | [Obsolete("The corlib is not always called 'mscorlib' (as returned by this property), but might be 'System.Runtime'.")] |
---|
32 | public static readonly IAssemblyReference Corlib = new DefaultAssemblyReference("mscorlib"); |
---|
33 | |
---|
34 | readonly string shortName; |
---|
35 | |
---|
36 | public DefaultAssemblyReference(string assemblyName) |
---|
37 | { |
---|
38 | int pos = assemblyName != null ? assemblyName.IndexOf(',') : -1; |
---|
39 | if (pos >= 0) |
---|
40 | shortName = assemblyName.Substring(0, pos); |
---|
41 | else |
---|
42 | shortName = assemblyName; |
---|
43 | } |
---|
44 | |
---|
45 | public IAssembly Resolve(ITypeResolveContext context) |
---|
46 | { |
---|
47 | IAssembly current = context.CurrentAssembly; |
---|
48 | if (current != null && string.Equals(shortName, current.AssemblyName, StringComparison.OrdinalIgnoreCase)) |
---|
49 | return current; |
---|
50 | foreach (IAssembly asm in context.Compilation.Assemblies) { |
---|
51 | if (string.Equals(shortName, asm.AssemblyName, StringComparison.OrdinalIgnoreCase)) |
---|
52 | return asm; |
---|
53 | } |
---|
54 | return null; |
---|
55 | } |
---|
56 | |
---|
57 | public override string ToString() |
---|
58 | { |
---|
59 | return shortName; |
---|
60 | } |
---|
61 | |
---|
62 | int ISupportsInterning.GetHashCodeForInterning() |
---|
63 | { |
---|
64 | unchecked { |
---|
65 | return shortName.GetHashCode(); |
---|
66 | } |
---|
67 | } |
---|
68 | |
---|
69 | bool ISupportsInterning.EqualsForInterning(ISupportsInterning other) |
---|
70 | { |
---|
71 | DefaultAssemblyReference o = other as DefaultAssemblyReference; |
---|
72 | return o != null && shortName == o.shortName; |
---|
73 | } |
---|
74 | |
---|
75 | [Serializable] |
---|
76 | sealed class CurrentAssemblyReference : IAssemblyReference |
---|
77 | { |
---|
78 | public IAssembly Resolve(ITypeResolveContext context) |
---|
79 | { |
---|
80 | IAssembly asm = context.CurrentAssembly; |
---|
81 | if (asm == null) |
---|
82 | throw new ArgumentException("A reference to the current assembly cannot be resolved in the compilation's global type resolve context."); |
---|
83 | return asm; |
---|
84 | } |
---|
85 | } |
---|
86 | } |
---|
87 | } |
---|