forked from dotnet/efcore
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathTypeExtensions.cs
More file actions
174 lines (164 loc) · 6.79 KB
/
Copy pathTypeExtensions.cs
File metadata and controls
174 lines (164 loc) · 6.79 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
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
// Copyright (c) .NET Foundation. All rights reserved.
// Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information.
using System;
using System.Collections.Generic;
using System.Globalization;
using System.Linq;
using System.Reflection;
using System.Text;
using JetBrains.Annotations;
namespace Microsoft.EntityFrameworkCore.Internal
{
/// <summary>
/// This API supports the Entity Framework Core infrastructure and is not intended to be used
/// directly from your code. This API may change or be removed in future releases.
/// </summary>
public static class TypeExtensions
{
private static readonly Dictionary<Type, string> _builtInTypeNames = new Dictionary<Type, string>
{
{ typeof(bool), "bool" },
{ typeof(byte), "byte" },
{ typeof(char), "char" },
{ typeof(decimal), "decimal" },
{ typeof(double), "double" },
{ typeof(float), "float" },
{ typeof(int), "int" },
{ typeof(long), "long" },
{ typeof(object), "object" },
{ typeof(sbyte), "sbyte" },
{ typeof(short), "short" },
{ typeof(string), "string" },
{ typeof(uint), "uint" },
{ typeof(ulong), "ulong" },
{ typeof(ushort), "ushort" }
};
/// <summary>
/// This API supports the Entity Framework Core infrastructure and is not intended to be used
/// directly from your code. This API may change or be removed in future releases.
/// </summary>
public static bool IsDefaultValue([NotNull] this Type type, [CanBeNull] object value)
=> (value == null) || value.Equals(type.GetDefaultValue());
/// <summary>
/// This API supports the Entity Framework Core infrastructure and is not intended to be used
/// directly from your code. This API may change or be removed in future releases.
/// </summary>
public static string ShortDisplayName([NotNull] this Type type)
=> type.DisplayName(fullName: false);
/// <summary>
/// This API supports the Entity Framework Core infrastructure and is not intended to be used
/// directly from your code. This API may change or be removed in future releases.
/// </summary>
public static string DisplayName([NotNull] this Type type, bool fullName = true)
{
var sb = new StringBuilder();
ProcessTypeName(type, sb, fullName);
return sb.ToString();
}
/// <summary>
/// This API supports the Entity Framework Core infrastructure and is not intended to be used
/// directly from your code. This API may change or be removed in future releases.
/// </summary>
public static FieldInfo GetFieldInfo([NotNull] this Type type, [NotNull] string fieldName)
{
var typesInHierarchy = type.GetTypesInHierarchy().ToList();
foreach (var typeInHierarchy in typesInHierarchy)
{
var fields = typeInHierarchy.GetRuntimeFields().ToDictionary(f => f.Name);
FieldInfo fieldInfo;
if (fields.TryGetValue(fieldName, out fieldInfo))
{
return fieldInfo;
}
}
return null;
}
private static void AppendGenericArguments(Type[] args, int startIndex, int numberOfArgsToAppend, StringBuilder sb, bool fullName)
{
var totalArgs = args.Length;
if (totalArgs >= startIndex + numberOfArgsToAppend)
{
sb.Append("<");
for (var i = startIndex; i < startIndex + numberOfArgsToAppend; i++)
{
ProcessTypeName(args[i], sb, fullName);
if (i + 1 < startIndex + numberOfArgsToAppend)
{
sb.Append(", ");
}
}
sb.Append(">");
}
}
private static void ProcessTypeName(Type t, StringBuilder sb, bool fullName)
{
if (t.GetTypeInfo().IsGenericType)
{
ProcessNestedGenericTypes(t, sb, fullName);
return;
}
if (_builtInTypeNames.ContainsKey(t))
{
sb.Append(_builtInTypeNames[t]);
}
else
{
sb.Append(fullName ? t.FullName : t.Name);
}
}
private static void ProcessNestedGenericTypes(Type t, StringBuilder sb, bool fullName)
{
var genericFullName = t.GetGenericTypeDefinition().FullName;
var genericSimpleName = t.GetGenericTypeDefinition().Name;
var parts = genericFullName.Split('+');
var genericArguments = t.GetTypeInfo().GenericTypeArguments;
var index = 0;
var totalParts = parts.Length;
if (totalParts == 1)
{
var part = parts[0];
var num = part.IndexOf('`');
if (num == -1)
{
return;
}
var name = part.Substring(0, num);
var numberOfGenericTypeArgs = int.Parse(part.Substring(num + 1), CultureInfo.InvariantCulture);
sb.Append(fullName ? name : genericSimpleName.Substring(0, genericSimpleName.IndexOf('`')));
AppendGenericArguments(genericArguments, index, numberOfGenericTypeArgs, sb, fullName);
return;
}
for (var i = 0; i < totalParts; i++)
{
var part = parts[i];
var num = part.IndexOf('`');
if (num != -1)
{
var name = part.Substring(0, num);
var numberOfGenericTypeArgs = int.Parse(part.Substring(num + 1), CultureInfo.InvariantCulture);
if (fullName || (i == totalParts - 1))
{
sb.Append(name);
AppendGenericArguments(genericArguments, index, numberOfGenericTypeArgs, sb, fullName);
}
if (fullName && (i != totalParts - 1))
{
sb.Append("+");
}
index += numberOfGenericTypeArgs;
}
else
{
if (fullName || (i == totalParts - 1))
{
sb.Append(part);
}
if (fullName && (i != totalParts - 1))
{
sb.Append("+");
}
}
}
}
}
}