1
// Copyright (c) 2010-2013 AlphaSierraPapa for the SharpDevelop Team
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:
9
// The above copyright notice and this permission notice shall be included in all copies or
10
// substantial portions of the Software.
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.
22
namespace ICSharpCode.NRefactory.TypeSystem.Implementation
25
/// Type Reference used when the fully qualified type name is known.
28
public sealed class GetClassTypeReference : ITypeReference, ISupportsInterning
30
readonly IAssemblyReference assembly;
31
readonly FullTypeName fullTypeName;
34
/// Creates a new GetClassTypeReference that searches a type definition.
36
/// <param name="fullTypeName">The full name of the type.</param>
37
/// <param name="assembly">A reference to the assembly containing this type.
38
/// If this parameter is null, the GetClassTypeReference will search in all
39
/// assemblies belonging to the compilation.
41
public GetClassTypeReference(FullTypeName fullTypeName, IAssemblyReference assembly = null)
43
this.fullTypeName = fullTypeName;
44
this.assembly = assembly;
48
/// Creates a new GetClassTypeReference that searches a top-level type in all assemblies.
50
/// <param name="namespaceName">The namespace name containing the type, e.g. "System.Collections.Generic".</param>
51
/// <param name="name">The name of the type, e.g. "List".</param>
52
/// <param name="typeParameterCount">The number of type parameters, (e.g. 1 for List<T>).</param>
53
public GetClassTypeReference(string namespaceName, string name, int typeParameterCount = 0)
55
this.fullTypeName = new TopLevelTypeName(namespaceName, name, typeParameterCount);
59
/// Creates a new GetClassTypeReference that searches a top-level type in the specified assembly.
61
/// <param name="assembly">A reference to the assembly containing this type.
62
/// If this parameter is null, the GetClassTypeReference will search in all assemblies belonging to the ICompilation.</param>
63
/// <param name="namespaceName">The namespace name containing the type, e.g. "System.Collections.Generic".</param>
64
/// <param name="name">The name of the type, e.g. "List".</param>
65
/// <param name="typeParameterCount">The number of type parameters, (e.g. 1 for List<T>).</param>
66
public GetClassTypeReference(IAssemblyReference assembly, string namespaceName, string name, int typeParameterCount = 0)
68
this.assembly = assembly;
69
this.fullTypeName = new TopLevelTypeName(namespaceName, name, typeParameterCount);
73
/// Gets the assembly reference.
74
/// This property returns null if the GetClassTypeReference is searching in all assemblies
75
/// of the compilation.
77
public IAssemblyReference Assembly { get { return assembly; } }
80
/// Gets the full name of the type this reference is searching for.
82
public FullTypeName FullTypeName { get { return fullTypeName; } }
84
[Obsolete("Use the FullTypeName property instead. GetClassTypeReference now supports nested types, where the Namespace/Name/TPC tripel isn't sufficient for identifying the type.")]
85
public string Namespace { get { return fullTypeName.TopLevelTypeName.Namespace; } }
86
[Obsolete("Use the FullTypeName property instead. GetClassTypeReference now supports nested types, where the Namespace/Name/TPC tripel isn't sufficient for identifying the type.")]
87
public string Name { get { return fullTypeName.Name; } }
88
[Obsolete("Use the FullTypeName property instead. GetClassTypeReference now supports nested types, where the Namespace/Name/TPC tripel isn't sufficient for identifying the type.")]
89
public int TypeParameterCount { get { return fullTypeName.TypeParameterCount; } }
91
public IType Resolve(ITypeResolveContext context)
94
throw new ArgumentNullException("context");
97
if (assembly == null) {
98
if (context.CurrentAssembly != null) {
99
type = context.CurrentAssembly.GetTypeDefinition(fullTypeName);
102
var compilation = context.Compilation;
103
foreach (var asm in compilation.Assemblies) {
104
type = asm.GetTypeDefinition(fullTypeName);
110
IAssembly asm = assembly.Resolve(context);
112
type = asm.GetTypeDefinition(fullTypeName);
115
return type ?? new UnknownType(fullTypeName);
118
public override string ToString()
120
return fullTypeName.ToString() + (assembly != null ? ", " + assembly.ToString() : null);
123
int ISupportsInterning.GetHashCodeForInterning()
126
return 33 * assembly.GetHashCode() + fullTypeName.GetHashCode();
130
bool ISupportsInterning.EqualsForInterning(ISupportsInterning other)
132
GetClassTypeReference o = other as GetClassTypeReference;
133
return o != null && assembly == o.assembly && fullTypeName == o.fullTypeName;