-
Notifications
You must be signed in to change notification settings - Fork 7
Expand file tree
/
Copy pathProxyModelMetadata.cs
More file actions
206 lines (163 loc) · 6.92 KB
/
Copy pathProxyModelMetadata.cs
File metadata and controls
206 lines (163 loc) · 6.92 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
using Microsoft.AspNetCore.Http;
using NetCoreStack.Proxy.Internal;
using System;
using System.Collections;
using System.Collections.Generic;
using System.ComponentModel;
using System.Reflection;
namespace NetCoreStack.Proxy
{
public class ProxyModelMetadata: IEquatable<ProxyModelMetadata>
{
private int? _hashCode;
protected ProxyModelMetadataIdentity Identity { get; }
public Type ModelType => Identity.ModelType;
public Type ContainerType => Identity.ContainerType;
public ProxyModelMetadata ElementType { get; private set; }
public Type UnderlyingOrModelType { get; private set; }
public PropertyInfo PropertyInfo { get; private set; }
public ProxyModelMetadataKind MetadataKind => Identity.MetadataKind;
public string PropertyName => Identity.Name;
public bool IsFormFile { get; private set; }
public bool IsSimpleType { get; private set; }
public bool IsSystemObject { get; private set; }
public bool IsComplexType { get; private set; }
public bool IsNullableValueType { get; private set; }
public bool IsCollectionType { get; private set; }
public bool IsEnumerableType { get; private set; }
public bool IsReferenceType { get; private set; }
public bool IsReferenceOrNullableType { get; private set; }
public IList<ProxyModelMetadata> Properties { get; private set; }
public int PropertiesCount => Properties.Count;
public ProxyModelMetadata(ProxyModelMetadataIdentity identity)
{
Identity = identity;
Properties = new List<ProxyModelMetadata>();
InitializeTypeInformation();
}
public ProxyModelMetadata(PropertyInfo propertyInfo, ProxyModelMetadataIdentity identity)
:this(identity)
{
PropertyInfo = propertyInfo;
}
public bool IsSimple(Type type)
{
var typeInfo = type.GetTypeInfo();
return typeInfo.IsPrimitive ||
typeInfo.IsEnum ||
type.Equals(typeof(decimal)) ||
type.Equals(typeof(string)) ||
type.Equals(typeof(DateTime)) ||
type.Equals(typeof(Guid)) ||
type.Equals(typeof(DateTimeOffset)) ||
type.Equals(typeof(TimeSpan)) ||
type.Equals(typeof(Uri));
}
private void InitializeTypeInformation()
{
var typeInfo = ModelType.GetTypeInfo();
IsComplexType = !TypeDescriptor.GetConverter(ModelType).CanConvertFrom(typeof(string));
IsNullableValueType = Nullable.GetUnderlyingType(ModelType) != null;
IsReferenceType = !typeInfo.IsValueType;
IsReferenceOrNullableType = !typeInfo.IsValueType || IsNullableValueType;
UnderlyingOrModelType = Nullable.GetUnderlyingType(ModelType) ?? ModelType;
IsFormFile = typeof(IFormFile).IsAssignableFrom(ModelType);
IsSimpleType = IsSimple(ModelType);
IsSystemObject = ModelType == typeof(object);
var collectionType = ClosedGenericMatcher.ExtractGenericInterface(ModelType, typeof(ICollection<>));
IsCollectionType = collectionType != null;
if (ModelType == typeof(string) || !typeof(IEnumerable).IsAssignableFrom(ModelType))
{
// Do nothing, not Enumerable.
}
else if (ModelType.IsArray)
{
IsEnumerableType = true;
var elementType = ModelType.GetElementType();
PropertyInfo propertyInfo = ContainerType?.GetProperty(PropertyName);
if (propertyInfo != null)
{
ElementType = new ProxyModelMetadata(propertyInfo, ProxyModelMetadataIdentity.ForProperty(elementType, propertyInfo.Name, ModelType));
}
IsFormFile = typeof(IFormFile).IsAssignableFrom(elementType);
}
else
{
IsEnumerableType = true;
var enumerableType = ClosedGenericMatcher.ExtractGenericInterface(ModelType, typeof(IEnumerable<>));
var elementType = enumerableType?.GenericTypeArguments[0];
if (elementType != null)
{
IsFormFile = typeof(IFormFile).IsAssignableFrom(elementType);
}
if (elementType == null)
{
// ModelType implements IEnumerable but not IEnumerable<T>.
elementType = typeof(object);
}
PropertyInfo propertyInfo = ContainerType?.GetProperty(PropertyName);
if (propertyInfo != null)
{
ElementType = new ProxyModelMetadata(propertyInfo, ProxyModelMetadataIdentity.ForProperty(elementType, propertyInfo.Name, ModelType));
}
}
if (IsNullableValueType)
{
var elementType = Nullable.GetUnderlyingType(ModelType);
PropertyInfo pInfo = ContainerType?.GetProperty(PropertyName);
if (pInfo != null)
{
ElementType = new ProxyModelMetadata(pInfo, ProxyModelMetadataIdentity.ForProperty(elementType, pInfo.Name, ModelType));
}
}
if (IsComplexType && IsReferenceOrNullableType &&
(!IsEnumerableType && !IsCollectionType) &&
ModelType.Name != typeof(object).Name)
{
PropertyInfo[] properties = ModelType.GetProperties();
List<ProxyModelMetadata> metadataList = new List<ProxyModelMetadata>();
foreach (var prop in properties)
{
metadataList.Add(new ProxyModelMetadata(prop, ProxyModelMetadataIdentity.ForProperty(prop.PropertyType, prop.Name, ModelType)));
}
Properties = metadataList;
}
}
public bool Equals(ProxyModelMetadata other)
{
if (object.ReferenceEquals(this, other))
{
return true;
}
if (other == null)
{
return false;
}
else
{
return Identity.Equals(other.Identity);
}
}
public override bool Equals(object obj)
{
return Equals(obj as ProxyModelMetadata);
}
public override int GetHashCode()
{
if (_hashCode == null)
{
_hashCode = Identity.GetHashCode();
}
return _hashCode.Value;
}
public override string ToString()
{
var name = PropertyName;
if (ContainerType != null)
{
name = ContainerType.Name + " - " + name;
}
return name;
}
}
}