This repository has been archived by the owner on Jul 18, 2023. It is now read-only.
/
Guard.Interop.cs
174 lines (156 loc) · 6.6 KB
/
Guard.Interop.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
#nullable enable
namespace Dawn
{
using System;
using System.Collections.Generic;
using System.Linq;
using System.Reflection;
using System.Runtime.CompilerServices;
/// <content>Provides utilities to support legacy frameworks.</content>
public static partial class Guard
{
/// <summary>Determines whether the specified type is a value type.</summary>
/// <param name="type">The type to check.</param>
/// <returns>
/// <c>true</c>, if <paramref name="type" /> represents a value type; otherwise, <c>false</c>.
/// </returns>
[MethodImpl(MethodImplOptions.AggressiveInlining)]
private static bool IsValueType(this Type type)
{
#if NETSTANDARD1_0
return type.GetTypeInfo().IsValueType;
#else
return type.IsValueType;
#endif
}
/// <summary>Determines whether the specified type is a generic type.</summary>
/// <param name="type">The type to check.</param>
/// <param name="definition">The type definition.</param>
/// <returns>
/// <c>true</c>, if <paramref name="type" /> represents a generic type with the specified
/// definition; otherwise, <c>false</c>.
/// </returns>
[MethodImpl(MethodImplOptions.AggressiveInlining)]
private static bool IsGenericType(this Type type, Type definition)
{
#if NETSTANDARD1_0
var info = type.GetTypeInfo();
return info.IsGenericType && info.GetGenericTypeDefinition() == definition;
#else
return type.IsGenericType && type.GetGenericTypeDefinition() == definition;
#endif
}
/// <summary>Determines whether the specified type is an enum.</summary>
/// <param name="type">The type to check.</param>
/// <returns>
/// <c>true</c>, if <paramref name="type" /> represents an enumeration; otherwise, <c>false</c>.
/// </returns>
[MethodImpl(MethodImplOptions.AggressiveInlining)]
private static bool IsEnum(this Type type)
{
#if NETSTANDARD1_0
return type.GetTypeInfo().IsEnum;
#else
return type.IsEnum;
#endif
}
/// <summary>Returns the type from which the specified type directly inherits.</summary>
/// <param name="type">The type whose base type to return.</param>
/// <returns>
/// The type from which the <paramref name="type" /> directly inherits, if there is one;
/// otherwise, <c>null</c>.
/// </returns>
[MethodImpl(MethodImplOptions.AggressiveInlining)]
private static Type? GetBaseType(this Type type)
{
#if NETSTANDARD1_0
return type.GetTypeInfo().BaseType;
#else
return type.BaseType;
#endif
}
/// <summary>Returns the getter of the property with the specified name.</summary>
/// <param name="type">The type that the property belongs to.</param>
/// <param name="name">The name of the property.</param>
/// <returns>
/// The getter of the property with the specified name, if it can be found in
/// <paramref name="type" />; otherwise, <c>null</c>.
/// </returns>
[MethodImpl(MethodImplOptions.AggressiveInlining)]
private static MethodInfo? GetPropertyGetter(this Type type, string name)
{
#if NETSTANDARD1_0
return type.GetTypeInfo().GetDeclaredProperty(name)?.GetMethod;
#else
return type.GetProperty(name)?.GetGetMethod();
#endif
}
#if NETSTANDARD1_0
[MethodImpl(MethodImplOptions.AggressiveInlining)]
private static bool IsAssignableFrom(this Type type, Type c)
=> type.GetTypeInfo().IsAssignableFrom(c.GetTypeInfo());
[MethodImpl(MethodImplOptions.AggressiveInlining)]
private static bool IsSubclassOf(this Type type, Type baseType)
=> type.GetTypeInfo().IsSubclassOf(baseType);
[MethodImpl(MethodImplOptions.AggressiveInlining)]
private static Type[] GetGenericArguments(this Type type)
=> type.GetTypeInfo().GenericTypeArguments;
[MethodImpl(MethodImplOptions.AggressiveInlining)]
private static IEnumerable<Type> GetInterfaces(this Type type)
=> type.GetTypeInfo().ImplementedInterfaces;
[MethodImpl(MethodImplOptions.AggressiveInlining)]
private static Type? GetNestedType(this Type type, string name)
=> type.GetTypeInfo().DeclaredNestedTypes.FirstOrDefault(t => t.Name == name)?.AsType();
[MethodImpl(MethodImplOptions.AggressiveInlining)]
private static FieldInfo GetField(this Type type, string name)
=> type.GetTypeInfo().GetDeclaredField(name);
private static ConstructorInfo GetConstructor(this Type type, Type[] arguments)
{
return type.GetTypeInfo().DeclaredConstructors
.FirstOrDefault(c => c.IsPublic && c
.GetParameters()
.Select(p => p.ParameterType)
.SequenceEqual(arguments));
}
[MethodImpl(MethodImplOptions.AggressiveInlining)]
private static MethodInfo GetMethod(this Type type, string name, Type[] arguments)
=> type.GetRuntimeMethod(name, arguments);
#endif
/// <summary>Provides a cached, empty array.</summary>
/// <typeparam name="T">The type of the array.</typeparam>
private static class Array<T>
{
/// <summary>Gets an empty array.</summary>
#if NETSTANDARD1_0
public static T[] Empty { get; } = new T[0];
#else
public static T[] Empty => Array.Empty<T>();
#endif
}
}
}
#if NETSTANDARD1_0
namespace System.Runtime.InteropServices
{
/// <summary>Required to use "in" parameters on .NET Standard 1.0.</summary>
[AttributeUsage(AttributeTargets.Parameter, Inherited = false)]
public sealed class InAttribute : Attribute
{
}
}
#endif
#if NETSTANDARD1_0 || NETSTANDARD2_0
namespace System.Diagnostics.CodeAnalysis
{
/// <summary>Required for reference nullability annotations.</summary>
[AttributeUsage(AttributeTargets.Parameter, Inherited = false)]
public sealed class NotNullWhenAttribute : Attribute
{
/// <summary>Initializes a new instance of the <see cref="NotNullWhenAttribute" /> class.</summary>
/// <param name="returnValue">If the method returns this value, the associated parameter will not be null.</param>
public NotNullWhenAttribute(bool returnValue) => this.ReturnValue = returnValue;
/// <summary>Gets the return value condition.</summary>
public bool ReturnValue { get; }
}
}
#endif