-
Notifications
You must be signed in to change notification settings - Fork 25
/
Copy pathTypeFunctions.AllReferencedTypes.cs
205 lines (181 loc) · 5.5 KB
/
TypeFunctions.AllReferencedTypes.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
using System;
using System.Linq;
using System.Collections.Generic;
using System.Text;
using NTypewriter.CodeModel.Traits;
namespace NTypewriter.CodeModel.Functions
{
/// <summary>
/// Areas of the search
/// </summary>
[Flags]
public enum SearchIn
{
/// <summary>
/// Properties
/// </summary>
Properties = 1,
/// <summary>
/// Methods
/// </summary>
Methods = 2,
/// <summary>
/// Fields
/// </summary>
Fields = 4,
/// <summary>
/// BaseClass
/// </summary>
BaseClass = 8,
/// <summary>
/// Implemented interfaces
/// </summary>
Interfaces = 16,
/// <summary>
/// All
/// </summary>
All = 127
}
/// <summary>
/// Set of functions that operates on IType
/// </summary>
public static partial class TypeFunctions
{
/// <summary>
/// Returns all types that are used in definition of a given type.
/// </summary>
public static IEnumerable<IType> AllReferencedTypes(this IType type, SearchIn searchIn = SearchIn.All)
{
var foundTypes = new HashSet<IType>(new TypeComparer());
if (searchIn.HasFlag(SearchIn.Properties) && (type is IHaveProperties typeWithProperties))
{
InspectProperties(foundTypes, typeWithProperties.Properties);
}
if (searchIn.HasFlag(SearchIn.Methods) && (type is IHaveMethods typeWithtMethods))
{
InspectMethods(foundTypes, typeWithtMethods.Methods);
}
if (searchIn.HasFlag(SearchIn.Fields) && (type is IHaveFields typeWithFields))
{
InspectFields(foundTypes, typeWithFields.Fields);
}
if (type is IClass @class)
{
if (searchIn.HasFlag(SearchIn.BaseClass))
{
if (@class.HasBaseClass)
{
InspectType(foundTypes, @class.BaseClass);
}
}
}
if (type is IType)
{
if (searchIn.HasFlag(SearchIn.Interfaces))
{
foreach (var @interface in type.Interfaces)
{
InspectType(foundTypes, @interface);
}
}
}
if (foundTypes.Contains(type))
{
foundTypes.Remove(type);
}
return foundTypes.ToList();
}
private static void InspectProperties(HashSet<IType> foundTypes, IEnumerable<IProperty> properties)
{
foreach (var property in properties)
{
InspectType(foundTypes, property.Type);
}
}
private static void InspectMethods(HashSet<IType> foundTypes, IEnumerable<IMethod> methods)
{
foreach (var method in methods)
{
InspectType(foundTypes, method.ReturnType);
foreach (var parameter in method.Parameters)
{
if (IsNotFromServices(parameter))
{
InspectType(foundTypes, parameter.Type);
}
}
}
}
private static void InspectFields(HashSet<IType> foundTypes, IEnumerable<IField> fields)
{
foreach (var field in fields)
{
InspectType(foundTypes, field.Type);
}
}
private static void InspectType(HashSet<IType> foundTypes, IType type)
{
if (type.IsErrorType)
{
return;
}
if (foundTypes.Contains(type))
{
return;
}
if (type.IsPrimitive)
{
return;
}
if (type.IsGeneric)
{
foreach (var typeArgument in type.TypeArguments)
{
InspectType(foundTypes, typeArgument);
}
}
if (type.IsArray)
{
InspectType(foundTypes, type.ArrayType);
return;
}
if ((type.IsNullable) && (type.IsSimple()))
{
return;
}
if (type.IsDynamic)
{
return;
}
if (!string.IsNullOrEmpty(type.Namespace))
{
if (type.Namespace.StartsWith("System.") || type.Namespace.StartsWith("Microsoft.") ||
type.Namespace.Equals("System") || type.Namespace.Equals("Microsoft")
)
{
return;
}
}
foundTypes.Add(type);
}
static bool IsNotFromServices(IParameter p)
{
if (p.Attributes.Any(x => x.Name == "FromServices"))
{
return false;
}
return true;
}
}
class TypeComparer : IEqualityComparer<IType>
{
public bool Equals(IType x, IType y)
{
return x.FullName.Equals(y.FullName);
}
public int GetHashCode(IType obj)
{
return obj.FullName.GetHashCode();
}
}
}