forked from ServiceStack/ServiceStack
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathTypeExtensions.cs
More file actions
78 lines (69 loc) · 2.42 KB
/
TypeExtensions.cs
File metadata and controls
78 lines (69 loc) · 2.42 KB
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
using System;
using System.Collections.Generic;
namespace ServiceStack
{
public static class TypeExtensions
{
public static Type[] GetReferencedTypes(this Type type)
{
var refTypes = new HashSet<Type> { type };
AddReferencedTypes(type, refTypes);
return refTypes.ToArray();
}
public static void AddReferencedTypes(Type type, HashSet<Type> refTypes)
{
if (type.BaseType != null)
{
if (!refTypes.Contains(type.BaseType))
{
refTypes.Add(type.BaseType);
AddReferencedTypes(type.BaseType, refTypes);
}
if (!type.BaseType.GetGenericArguments().IsEmpty())
{
foreach (var arg in type.BaseType.GetGenericArguments())
{
if (!refTypes.Contains(arg))
{
refTypes.Add(arg);
AddReferencedTypes(arg, refTypes);
}
}
}
}
var properties = type.GetProperties();
if (!properties.IsEmpty())
{
foreach (var p in properties)
{
if (!refTypes.Contains(p.PropertyType))
{
refTypes.Add(p.PropertyType);
AddReferencedTypes(type, refTypes);
}
var args = p.PropertyType.GetGenericArguments();
if (!args.IsEmpty())
{
foreach (var arg in args)
{
if (!refTypes.Contains(arg))
{
refTypes.Add(arg);
AddReferencedTypes(arg, refTypes);
}
}
}
else if (p.PropertyType.IsArray)
{
var elType = p.PropertyType.GetElementType();
if (!refTypes.Contains(elType))
{
refTypes.Add(elType);
AddReferencedTypes(elType, refTypes);
}
}
}
}
}
}
}