-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathTypeHelper.cs
More file actions
116 lines (97 loc) · 3.45 KB
/
Copy pathTypeHelper.cs
File metadata and controls
116 lines (97 loc) · 3.45 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
using System;
using System.Collections;
using System.Collections.Generic;
using System.Linq;
using System.Linq.Expressions;
using System.Reflection;
namespace NetCoreStack.Contracts
{
public static class TypeHelper
{
public static bool IsEntity(Type type)
{
return typeof(IEntity).IsAssignableFrom(type);
}
public static bool IsIdModel(Type type)
{
if (typeof(IdModel).IsAssignableFrom(type))
{
return true;
}
return false;
}
public static bool IsKeyModel(Type type)
{
if (typeof(KeyModel).IsAssignableFrom(type))
{
return true;
}
return false;
}
public static string GetMemberName<T, TValue>(Expression<Func<T, TValue>> memberAccess)
{
return ((MemberExpression)memberAccess.Body).Member.Name;
}
public static void AddInterface(List<Type> types, Type type)
{
if (!types.Contains(type))
{
types.Add(type);
Type[] interfaces = type.GetInterfaces();
for (int i = 0; i < interfaces.Length; i++)
{
Type intrfc = interfaces[i];
AddInterface(types, intrfc);
}
}
}
public static IList<T> CreateElementTypeAsGenericList<T>()
{
return (IList<T>)Activator.CreateInstance(typeof(List<>).MakeGenericType(new Type[] { typeof(T) }));
}
public static IEnumerable<T> CreateElementTypeAsEnumerable<T>()
{
return (IEnumerable<T>)Activator.CreateInstance(typeof(IEnumerable<>).MakeGenericType(new Type[] { typeof(T) }));
}
public static Type GetElementType(Type enumerableType)
{
return GetElementType(enumerableType, null);
}
public static Type GetElementType(Type enumerableType, IEnumerable enumerable)
{
if (enumerableType.HasElementType)
{
return enumerableType.GetElementType();
}
if (enumerableType.IsGenericType() &&
enumerableType.GetGenericTypeDefinition() == typeof(IEnumerable<>))
{
return enumerableType.GetTypeInfo().GenericTypeArguments[0];
}
Type ienumerableType = GetIEnumerableType(enumerableType);
if (ienumerableType != null)
{
return ienumerableType.GetTypeInfo().GenericTypeArguments[0];
}
if (typeof(IEnumerable).IsAssignableFrom(enumerableType))
{
var first = enumerable?.Cast<object>().FirstOrDefault();
return first?.GetType() ?? typeof(object);
}
throw new ArgumentException($"Unable to find the element type for type '{enumerableType}'.", nameof(enumerableType));
}
private static Type GetIEnumerableType(Type enumerableType)
{
try
{
return enumerableType.GetTypeInfo().ImplementedInterfaces.FirstOrDefault(t => t.Name == "IEnumerable`1");
}
catch (AmbiguousMatchException)
{
if (enumerableType.BaseType() != typeof(object))
return GetIEnumerableType(enumerableType.BaseType());
return null;
}
}
}
}