-
-
Notifications
You must be signed in to change notification settings - Fork 10
Expand file tree
/
Copy pathTypeExtensions.cs
More file actions
73 lines (62 loc) · 1.85 KB
/
TypeExtensions.cs
File metadata and controls
73 lines (62 loc) · 1.85 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
using System;
using System.Collections.Generic;
using System.Linq;
namespace Simplify.Web.System;
/// <summary>
/// Provides the <see cref="Type" /> extensions.
/// </summary>
public static class TypeExtensions
{
/// <summary>
/// Determines whether one type is derivative of another.
/// </summary>
/// <param name="t">The type.</param>
/// <param name="type">The type.</param>
public static bool IsTypeOf(this Type t, Type type)
{
switch (t.IsGenericType)
{
case false when t == type:
case true when t.GetGenericTypeDefinition() == type:
return true;
default:
return false;
}
}
/// <summary>
/// Determines whether the type is derived from one of the specified types.
/// </summary>
/// <param name="t">The t.</param>
/// <param name="types">The types.</param>
/// <returns>
/// <c>true</c> if the type is derived from one of the specified types; otherwise, <c>false</c>.
/// </returns>
public static bool IsDerivedFrom(this Type t, params Type[] types) => Array.Exists(types, t.IsDerivedFrom);
/// <summary>
/// Determines whether the type is derived from other type.
/// </summary>
/// <param name="t">The t.</param>
/// <param name="type">The type.</param>
/// <returns>
/// <c>true</c> if the type is derived from other type; otherwise, <c>false</c>.
/// </returns>
public static bool IsDerivedFrom(this Type t, Type type)
{
if (t.IsAbstract)
return false;
if (t.BaseType == null)
return false;
if (t.BaseType.IsTypeOf(type))
return true;
if (t.BaseType.BaseType == null)
return false;
if (t.BaseType.BaseType.IsTypeOf(type))
return true;
return false;
}
/// <summary>
/// Gets the type names as string.
/// </summary>
/// <param name="types">The types.</param>
public static string GetTypeNamesAsString(this IEnumerable<Type> types) => string.Join(", ", types.Select(type => type.Name));
}