-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathTypeHelper.cs
More file actions
153 lines (129 loc) · 4.88 KB
/
Copy pathTypeHelper.cs
File metadata and controls
153 lines (129 loc) · 4.88 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
using Microsoft.Extensions.DependencyInjection;
using NetCoreStack.Common.Extensions;
using System;
using System.Collections;
using System.Collections.Generic;
using System.Linq;
using System.Linq.Expressions;
using System.Reflection;
namespace NetCoreStack.Common
{
public static class TypeHelper
{
public static bool IsDependency(Type type)
{
return typeof(IDependency).IsAssignableFrom(type) ||
type.GetTypeInfo().GetCustomAttribute<ApiRouteAttribute>() != null;
}
public static bool IsEntity(Type type)
{
return typeof(EntityBase).IsAssignableFrom(type);
}
public static bool IsViewModel(Type type)
{
return typeof(BaseViewModel).IsAssignableFrom(type);
}
public static bool IsCompositeType(Type type)
{
return typeof(BaseCompositeType).IsAssignableFrom(type);
}
public static bool IsApi(Type type)
{
return typeof(IApiContract).IsAssignableFrom(type) &&
type.GetTypeInfo().GetCustomAttribute<ApiRouteAttribute>() != null;
}
public static bool IsTransient(Type type)
{
return typeof(ITransientDependency).IsAssignableFrom(type);
}
public static bool IsSingleton(Type type)
{
return typeof(ISingletonDependency).IsAssignableFrom(type);
}
public static bool IsApiDependency(Type type)
{
return typeof(IDependency).IsAssignableFrom(type) ||
type.GetTypeInfo().GetCustomAttribute<ApiRouteAttribute>() != null;
}
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 type2 = interfaces[i];
AddInterface(types, type2);
}
}
}
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));
}
public static void Register(IServiceCollection services, Type serviceType, Type implementationType)
{
if (IsDependency(serviceType))
{
services.AddScoped(serviceType, implementationType);
}
else if (IsTransient(serviceType))
{
services.AddTransient(serviceType, implementationType);
}
else if (IsSingleton(serviceType))
{
services.AddSingleton(serviceType, implementationType);
}
}
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;
}
}
}
}