- Notifications
You must be signed in to change notification settings - Fork 549
/
Copy pathPropertyInfoExtensions.cs
242 lines (195 loc) · 9.34 KB
/
PropertyInfoExtensions.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
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
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
// Copyright (c) Microsoft Open Technologies, Inc. All rights reserved. See License.txt in the project root for license information.
#if SQLSERVER
namespaceSystem.Data.Entity.SqlServer.Utilities
#elif EF_FUNCTIONALS
namespaceSystem.Data.Entity.Functionals.Utilities
#else
namespaceSystem.Data.Entity.Utilities
#endif
{
usingSystem.Collections.Generic;
usingSystem.Data.Entity.Core.Metadata.Edm;
usingSystem.Data.Entity.ModelConfiguration.Mappers;
usingSystem.Diagnostics;
usingSystem.Linq;
usingSystem.Reflection;
internalstaticclassPropertyInfoExtensions
{
publicstaticboolIsSameAs(thisPropertyInfopropertyInfo,PropertyInfootherPropertyInfo)
{
DebugCheck.NotNull(propertyInfo);
DebugCheck.NotNull(otherPropertyInfo);
return(propertyInfo==otherPropertyInfo)||
(propertyInfo.Name==otherPropertyInfo.Name
&&(propertyInfo.DeclaringType==otherPropertyInfo.DeclaringType
||propertyInfo.DeclaringType.IsSubclassOf(otherPropertyInfo.DeclaringType)
||otherPropertyInfo.DeclaringType.IsSubclassOf(propertyInfo.DeclaringType)
||propertyInfo.DeclaringType.GetInterfaces().Contains(otherPropertyInfo.DeclaringType)
||otherPropertyInfo.DeclaringType.GetInterfaces().Contains(propertyInfo.DeclaringType)));
}
publicstaticboolContainsSame(thisIEnumerable<PropertyInfo>enumerable,PropertyInfopropertyInfo)
{
DebugCheck.NotNull(enumerable);
DebugCheck.NotNull(propertyInfo);
returnenumerable.Any(propertyInfo.IsSameAs);
}
publicstaticboolIsValidStructuralProperty(thisPropertyInfopropertyInfo)
{
DebugCheck.NotNull(propertyInfo);
returnpropertyInfo.IsValidInterfaceStructuralProperty()
&&!propertyInfo.Getter().IsAbstract;
}
publicstaticboolIsValidInterfaceStructuralProperty(thisPropertyInfopropertyInfo)
{
DebugCheck.NotNull(propertyInfo);
returnpropertyInfo.CanRead
&&(propertyInfo.CanWriteExtended()||propertyInfo.PropertyType.IsCollection())
&&propertyInfo.GetIndexParameters().Length==0
&&propertyInfo.PropertyType.IsValidStructuralPropertyType();
}
publicstaticboolIsValidEdmScalarProperty(thisPropertyInfopropertyInfo)
{
DebugCheck.NotNull(propertyInfo);
returnIsValidInterfaceStructuralProperty(propertyInfo)
&&propertyInfo.PropertyType.IsValidEdmScalarType();
}
publicstaticboolIsValidEdmNavigationProperty(thisPropertyInfopropertyInfo)
{
DebugCheck.NotNull(propertyInfo);
TypeelementType;
returnIsValidInterfaceStructuralProperty(propertyInfo)
&&((propertyInfo.PropertyType.IsCollection(outelementType)&&elementType.IsValidStructuralType())
||propertyInfo.PropertyType.IsValidStructuralType());
}
publicstaticEdmPropertyAsEdmPrimitiveProperty(thisPropertyInfopropertyInfo)
{
DebugCheck.NotNull(propertyInfo);
varpropertyType=propertyInfo.PropertyType;
varisNullable=propertyType.TryUnwrapNullableType(outpropertyType)||!propertyType.IsValueType();
PrimitiveTypeprimitiveType;
if(propertyType.IsPrimitiveType(outprimitiveType))
{
varproperty=EdmProperty.CreatePrimitive(propertyInfo.Name,primitiveType);
property.Nullable=isNullable;
returnproperty;
}
returnnull;
}
publicstaticboolCanWriteExtended(thisPropertyInfopropertyInfo)
{
DebugCheck.NotNull(propertyInfo);
if(propertyInfo.CanWrite)
{
returntrue;
}
vardeclaredProperty=GetDeclaredProperty(propertyInfo);
returndeclaredProperty!=null&&declaredProperty.CanWrite;
}
publicstaticPropertyInfoGetPropertyInfoForSet(thisPropertyInfopropertyInfo)
{
DebugCheck.NotNull(propertyInfo);
returnpropertyInfo.CanWrite?propertyInfo:GetDeclaredProperty(propertyInfo)??propertyInfo;
}
privatestaticPropertyInfoGetDeclaredProperty(PropertyInfopropertyInfo)
{
Debug.Assert(propertyInfo.DeclaringType!=null);
returnpropertyInfo.DeclaringType==propertyInfo.ReflectedType
?propertyInfo
:propertyInfo
.DeclaringType
.GetInstanceProperties()
.SingleOrDefault(
p =>p.Name==propertyInfo.Name
&&p.DeclaringType==propertyInfo.DeclaringType
&&!p.GetIndexParameters().Any()
&&p.PropertyType==propertyInfo.PropertyType);
}
publicstaticIEnumerable<PropertyInfo>GetPropertiesInHierarchy(thisPropertyInfoproperty)
{
DebugCheck.NotNull(property);
varcollection=newList<PropertyInfo>{property};
CollectProperties(property,collection);
returncollection.Distinct();
}
privatestaticvoidCollectProperties(PropertyInfoproperty,IList<PropertyInfo>collection)
{
DebugCheck.NotNull(property);
DebugCheck.NotNull(collection);
FindNextProperty(property,collection,getter:true);
FindNextProperty(property,collection,getter:false);
}
privatestaticvoidFindNextProperty(PropertyInfoproperty,IList<PropertyInfo>collection,boolgetter)
{
DebugCheck.NotNull(property);
DebugCheck.NotNull(collection);
varmethod=getter?property.Getter():property.Setter();
if(method!=null)
{
varnextType=method.DeclaringType.BaseType();
if(nextType!=null&&nextType!=typeof(object))
{
varbaseMethod=method.GetBaseDefinition();
varnextProperty=
(frompinnextType.GetInstanceProperties()
letcandidateMethod=getter?p.Getter():p.Setter()
wherecandidateMethod!=null&&candidateMethod.GetBaseDefinition()==baseMethod
selectp).FirstOrDefault();
if(nextProperty!=null)
{
collection.Add(nextProperty);
CollectProperties(nextProperty,collection);
}
}
}
}
publicstaticMethodInfoGetter(thisPropertyInfoproperty)
{
DebugCheck.NotNull(property);
#if NET40
returnproperty.GetGetMethod(nonPublic:true);
#else
returnproperty.GetMethod;
#endif
}
publicstaticMethodInfoSetter(thisPropertyInfoproperty)
{
DebugCheck.NotNull(property);
#if NET40
returnproperty.GetSetMethod(nonPublic:true);
#else
returnproperty.SetMethod;
#endif
}
publicstaticboolIsStatic(thisPropertyInfoproperty)
{
DebugCheck.NotNull(property);
return(property.Getter()??property.Setter()).IsStatic;
}
publicstaticboolIsPublic(thisPropertyInfoproperty)
{
DebugCheck.NotNull(property);
// The MethodAttributes enum for member access has the following values:
// 1 Private
// 2 FamANDAssem
// 3 Assembly
// 4 Family
// 5 FamORAssem
// 6 Public
// Starting from the bottom, Public is more permissive than anything above it--meaning that
// if it can be accessed publically then it can be accessed by anything. Likewise,
// FamORAssem is more permissive than anything above it. Assembly can be more permissive
// than Family and vice versa. (However, at least in C# and VB a property setter cannot be
// Assembly while the getter is Family or vice versa.) Since there is no real permissive winner
// here, we will use the enum order and call Family more permissive than Assembly, but this is
// a largely arbitrary choice. Finally, FamANDAssem is more permissive than private, which is the
// least permissive.
// We can therefore use this order to infer the accessibility of the property.
vargetter=property.Getter();
vargetterAccess=getter==null?MethodAttributes.Private:(getter.Attributes&MethodAttributes.MemberAccessMask);
varsetter=property.Setter();
varsetterAccess=setter==null?MethodAttributes.Private:(setter.Attributes&MethodAttributes.MemberAccessMask);
varpropertyAccess=getterAccess>setterAccess?getterAccess:setterAccess;
returnpropertyAccess==MethodAttributes.Public;
}
}
}