forked from dotnet/efcore
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSharedTypeExtensions.cs
More file actions
215 lines (181 loc) · 7.73 KB
/
Copy pathSharedTypeExtensions.cs
File metadata and controls
215 lines (181 loc) · 7.73 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
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
206
207
208
209
210
211
212
213
214
215
// 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.Collections.Generic;
using System.Diagnostics;
using System.Linq;
using System.Reflection;
// ReSharper disable once CheckNamespace
namespace System
{
[DebuggerStepThrough]
internal static class SharedTypeExtensions
{
public static Type UnwrapNullableType(this Type type) => Nullable.GetUnderlyingType(type) ?? type;
public static bool IsNullableType(this Type type)
{
var typeInfo = type.GetTypeInfo();
return !typeInfo.IsValueType
|| (typeInfo.IsGenericType
&& typeInfo.GetGenericTypeDefinition() == typeof(Nullable<>));
}
public static Type MakeNullable(this Type type)
=> type.IsNullableType()
? type
: typeof(Nullable<>).MakeGenericType(type);
public static bool IsInteger(this Type type)
{
type = type.UnwrapNullableType();
return type == typeof(int)
|| type == typeof(long)
|| type == typeof(short)
|| type == typeof(byte)
|| type == typeof(uint)
|| type == typeof(ulong)
|| type == typeof(ushort)
|| type == typeof(sbyte)
|| type == typeof(char);
}
public static PropertyInfo GetAnyProperty(this Type type, string name)
{
var props = type.GetRuntimeProperties().Where(p => p.Name == name).ToList();
if (props.Count() > 1)
{
throw new AmbiguousMatchException();
}
return props.SingleOrDefault();
}
private static bool IsNonIntegerPrimitive(this Type type)
{
type = type.UnwrapNullableType();
return type == typeof(bool)
|| type == typeof(byte[])
|| type == typeof(DateTime)
|| type == typeof(DateTimeOffset)
|| type == typeof(decimal)
|| type == typeof(double)
|| type == typeof(float)
|| type == typeof(Guid)
|| type == typeof(string)
|| type == typeof(TimeSpan)
|| type.GetTypeInfo().IsEnum;
}
public static bool IsPrimitive(this Type type)
=> type.IsInteger() || type.IsNonIntegerPrimitive();
public static bool IsInstantiable(this Type type) => IsInstantiable(type.GetTypeInfo());
private static bool IsInstantiable(TypeInfo type)
=> !type.IsAbstract
&& !type.IsInterface
&& (!type.IsGenericType || !type.IsGenericTypeDefinition);
public static Type UnwrapEnumType(this Type type)
{
var isNullable = type.IsNullableType();
type = isNullable ? type.UnwrapNullableType() : type;
var underlyingEnumType = type.GetTypeInfo().IsEnum ? Enum.GetUnderlyingType(type) : type;
return isNullable ? MakeNullable(underlyingEnumType) : underlyingEnumType;
}
public static Type GetSequenceType(this Type type)
{
var sequenceType = TryGetSequenceType(type);
if (sequenceType == null)
{
// TODO: Add exception message
throw new ArgumentException();
}
return sequenceType;
}
public static Type TryGetSequenceType(this Type type)
=> type.TryGetElementType(typeof(IEnumerable<>))
?? type.TryGetElementType(typeof(IAsyncEnumerable<>));
public static Type TryGetElementType(this Type type, Type interfaceOrBaseType)
{
if (!type.GetTypeInfo().IsGenericTypeDefinition)
{
var types = GetGenericTypeImplementations(type, interfaceOrBaseType).ToArray();
return types.Length == 1 ? types[0].GetTypeInfo().GenericTypeArguments.FirstOrDefault() : null;
}
return null;
}
public static IEnumerable<Type> GetGenericTypeImplementations(this Type type, Type interfaceOrBaseType)
{
var typeInfo = type.GetTypeInfo();
if (!typeInfo.IsGenericTypeDefinition)
{
return (interfaceOrBaseType.GetTypeInfo().IsInterface ? typeInfo.ImplementedInterfaces : type.GetBaseTypes())
.Union(new[] { type })
.Where(
t => t.GetTypeInfo().IsGenericType
&& t.GetGenericTypeDefinition() == interfaceOrBaseType);
}
return Enumerable.Empty<Type>();
}
public static IEnumerable<Type> GetBaseTypes(this Type type)
{
type = type.GetTypeInfo().BaseType;
while (type != null)
{
yield return type;
type = type.GetTypeInfo().BaseType;
}
}
public static ConstructorInfo GetDeclaredConstructor(this Type type, Type[] types)
{
types = types ?? new Type[0];
return type.GetTypeInfo().DeclaredConstructors
.SingleOrDefault(
c => !c.IsStatic
&& c.GetParameters().Select(p => p.ParameterType).SequenceEqual(types));
}
public static IEnumerable<PropertyInfo> GetPropertiesInHierarchy(this Type type, string name)
{
do
{
var typeInfo = type.GetTypeInfo();
var propertyInfo = typeInfo.GetDeclaredProperty(name);
if (propertyInfo != null
&& !(propertyInfo.GetMethod ?? propertyInfo.SetMethod).IsStatic)
{
yield return propertyInfo;
}
type = typeInfo.BaseType;
}
while (type != null);
}
private static readonly Dictionary<Type, object> _commonTypeDictionary = new Dictionary<Type, object>
{
{ typeof(int), default(int) },
{ typeof(Guid), default(Guid) },
{ typeof(DateTime), default(DateTime) },
{ typeof(DateTimeOffset), default(DateTimeOffset) },
{ typeof(long), default(long) },
{ typeof(bool), default(bool) },
{ typeof(double), default(double) },
{ typeof(short), default(short) },
{ typeof(float), default(float) },
{ typeof(byte), default(byte) },
{ typeof(char), default(char) },
{ typeof(uint), default(uint) },
{ typeof(ushort), default(ushort) },
{ typeof(ulong), default(ulong) },
{ typeof(sbyte), default(sbyte) }
};
public static object GetDefaultValue(this Type type)
{
if (!type.GetTypeInfo().IsValueType)
{
return null;
}
// A bit of perf code to avoid calling Activator.CreateInstance for common types and
// to avoid boxing on every call. This is about 50% faster than just calling CreateInstance
// for all value types.
object value;
return _commonTypeDictionary.TryGetValue(type, out value)
? value
: Activator.CreateInstance(type);
}
public static IEnumerable<TypeInfo> GetConstructibleTypes(this Assembly assembly)
=> assembly.DefinedTypes.Where(
t => !t.IsAbstract
&& !t.IsGenericType
&& t.DeclaredConstructors.Any(c => c.GetParameters().Length == 0 && c.IsPublic));
}
}