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.
20
using System.Collections.Generic;
23
namespace ICSharpCode.NRefactory.TypeSystem
26
/// Holds the name of a top-level type.
27
/// This struct cannot refer to nested classes.
30
public struct TopLevelTypeName : IEquatable<TopLevelTypeName>
32
readonly string namespaceName;
34
readonly int typeParameterCount;
36
public TopLevelTypeName(string namespaceName, string name, int typeParameterCount = 0)
38
if (namespaceName == null)
39
throw new ArgumentNullException("namespaceName");
41
throw new ArgumentNullException("name");
42
this.namespaceName = namespaceName;
44
this.typeParameterCount = typeParameterCount;
47
public TopLevelTypeName(string reflectionName)
49
int pos = reflectionName.LastIndexOf('.');
51
namespaceName = string.Empty;
52
name = reflectionName;
54
namespaceName = reflectionName.Substring(0, pos);
55
name = reflectionName.Substring(pos + 1);
57
name = ReflectionHelper.SplitTypeParameterCountFromReflectionName(name, out typeParameterCount);
60
public string Namespace {
61
get { return namespaceName; }
68
public int TypeParameterCount {
69
get { return typeParameterCount; }
72
public string ReflectionName {
74
StringBuilder b = new StringBuilder();
75
if (!string.IsNullOrEmpty(namespaceName)) {
76
b.Append(namespaceName);
80
if (typeParameterCount > 0) {
82
b.Append(typeParameterCount);
88
public override string ToString()
90
return this.ReflectionName;
93
public override bool Equals(object obj)
95
return (obj is TopLevelTypeName) && Equals((TopLevelTypeName)obj);
98
public bool Equals(TopLevelTypeName other)
100
return this.namespaceName == other.namespaceName && this.name == other.name && this.typeParameterCount == other.typeParameterCount;
103
public override int GetHashCode()
105
return (name != null ? name.GetHashCode() : 0) ^ (namespaceName != null ? namespaceName.GetHashCode() : 0) ^ typeParameterCount;
108
public static bool operator ==(TopLevelTypeName lhs, TopLevelTypeName rhs)
110
return lhs.Equals(rhs);
113
public static bool operator !=(TopLevelTypeName lhs, TopLevelTypeName rhs)
115
return !lhs.Equals(rhs);
120
public sealed class TopLevelTypeNameComparer : IEqualityComparer<TopLevelTypeName>
122
public static readonly TopLevelTypeNameComparer Ordinal = new TopLevelTypeNameComparer(StringComparer.Ordinal);
123
public static readonly TopLevelTypeNameComparer OrdinalIgnoreCase = new TopLevelTypeNameComparer(StringComparer.OrdinalIgnoreCase);
125
public readonly StringComparer NameComparer;
127
public TopLevelTypeNameComparer(StringComparer nameComparer)
129
this.NameComparer = nameComparer;
132
public bool Equals(TopLevelTypeName x, TopLevelTypeName y)
134
return x.TypeParameterCount == y.TypeParameterCount
135
&& NameComparer.Equals(x.Name, y.Name)
136
&& NameComparer.Equals(x.Namespace, y.Namespace);
139
public int GetHashCode(TopLevelTypeName obj)
141
return NameComparer.GetHashCode(obj.Name) ^ NameComparer.GetHashCode(obj.Namespace) ^ obj.TypeParameterCount;