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 | using System.Linq; |
---|
21 | using ICSharpCode.NRefactory.TypeSystem; |
---|
22 | using ICSharpCode.NRefactory.TypeSystem.Implementation; |
---|
23 | |
---|
24 | namespace ICSharpCode.NRefactory.Documentation |
---|
25 | { |
---|
26 | /// <summary> |
---|
27 | /// A type reference of the form 'Some.Namespace.TopLevelType.NestedType`n'. |
---|
28 | /// We do not know the boundary between namespace name and top level type, so we have to try |
---|
29 | /// all possibilities. |
---|
30 | /// The type parameter count only applies to the innermost type, all outer types must be non-generic. |
---|
31 | /// </summary> |
---|
32 | [Serializable] |
---|
33 | class GetPotentiallyNestedClassTypeReference : ITypeReference |
---|
34 | { |
---|
35 | readonly string typeName; |
---|
36 | readonly int typeParameterCount; |
---|
37 | |
---|
38 | public GetPotentiallyNestedClassTypeReference(string typeName, int typeParameterCount) |
---|
39 | { |
---|
40 | this.typeName = typeName; |
---|
41 | this.typeParameterCount = typeParameterCount; |
---|
42 | } |
---|
43 | |
---|
44 | public IType Resolve(ITypeResolveContext context) |
---|
45 | { |
---|
46 | string[] parts = typeName.Split('.'); |
---|
47 | var assemblies = new [] { context.CurrentAssembly }.Concat(context.Compilation.Assemblies); |
---|
48 | for (int i = parts.Length - 1; i >= 0; i--) { |
---|
49 | string ns = string.Join(".", parts, 0, i); |
---|
50 | string name = parts[i]; |
---|
51 | int topLevelTPC = (i == parts.Length - 1 ? typeParameterCount : 0); |
---|
52 | foreach (var asm in assemblies) { |
---|
53 | if (asm == null) |
---|
54 | continue; |
---|
55 | ITypeDefinition typeDef = asm.GetTypeDefinition(new TopLevelTypeName(ns, name, topLevelTPC)); |
---|
56 | for (int j = i + 1; j < parts.Length && typeDef != null; j++) { |
---|
57 | int tpc = (j == parts.Length - 1 ? typeParameterCount : 0); |
---|
58 | typeDef = typeDef.NestedTypes.FirstOrDefault(n => n.Name == parts[j] && n.TypeParameterCount == tpc); |
---|
59 | } |
---|
60 | if (typeDef != null) |
---|
61 | return typeDef; |
---|
62 | } |
---|
63 | } |
---|
64 | int idx = typeName.LastIndexOf('.'); |
---|
65 | if (idx < 0) |
---|
66 | return new UnknownType("", typeName, typeParameterCount); |
---|
67 | // give back a guessed namespace/type name |
---|
68 | return new UnknownType(typeName.Substring(0, idx), typeName.Substring(idx + 1), typeParameterCount); |
---|
69 | } |
---|
70 | } |
---|
71 | } |
---|