-
Notifications
You must be signed in to change notification settings - Fork 3.2k
/
ClrPropertyGetterFactory.cs
165 lines (146 loc) · 8.92 KB
/
ClrPropertyGetterFactory.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
// Licensed to the .NET Foundation under one or more agreements.
// The .NET Foundation licenses this file to you under the MIT license.
using System.Runtime.ExceptionServices;
using Microsoft.EntityFrameworkCore.Internal;
namespace Microsoft.EntityFrameworkCore.Metadata.Internal;
/// <summary>
/// This is an internal API that supports the Entity Framework Core infrastructure and not subject to
/// the same compatibility standards as public APIs. It may be changed or removed without notice in
/// any release. You should only use it directly in your code with extreme caution and knowing that
/// doing so can result in application failures when updating to a new Entity Framework Core release.
/// </summary>
public class ClrPropertyGetterFactory : ClrAccessorFactory<IClrPropertyGetter>
{
private ClrPropertyGetterFactory()
{
}
/// <summary>
/// This is an internal API that supports the Entity Framework Core infrastructure and not subject to
/// the same compatibility standards as public APIs. It may be changed or removed without notice in
/// any release. You should only use it directly in your code with extreme caution and knowing that
/// doing so can result in application failures when updating to a new Entity Framework Core release.
/// </summary>
public static readonly ClrPropertyGetterFactory Instance = new();
/// <summary>
/// This is an internal API that supports the Entity Framework Core infrastructure and not subject to
/// the same compatibility standards as public APIs. It may be changed or removed without notice in
/// any release. You should only use it directly in your code with extreme caution and knowing that
/// doing so can result in application failures when updating to a new Entity Framework Core release.
/// </summary>
public override IClrPropertyGetter Create(IPropertyBase property)
=> property as IClrPropertyGetter ?? CreateBase(property);
/// <summary>
/// This is an internal API that supports the Entity Framework Core infrastructure and not subject to
/// the same compatibility standards as public APIs. It may be changed or removed without notice in
/// any release. You should only use it directly in your code with extreme caution and knowing that
/// doing so can result in application failures when updating to a new Entity Framework Core release.
/// </summary>
protected override IClrPropertyGetter CreateGeneric<TEntity, TStructuralType, TValue>(
MemberInfo memberInfo,
IPropertyBase? propertyBase)
{
CreateExpressions<TEntity, TStructuralType, TValue>(
memberInfo, propertyBase,
out var getterExpression, out var hasSentinelExpression, out var structuralGetterExpression,
out var hasStructuralSentinelExpression);
return new ClrPropertyGetter<TEntity, TStructuralType, TValue>(
getterExpression.Compile(),
hasSentinelExpression.Compile(),
structuralGetterExpression.Compile(),
hasStructuralSentinelExpression.Compile());
}
/// <summary>
/// This is an internal API that supports the Entity Framework Core infrastructure and not subject to
/// the same compatibility standards as public APIs. It may be changed or removed without notice in
/// any release. You should only use it directly in your code with extreme caution and knowing that
/// doing so can result in application failures when updating to a new Entity Framework Core release.
/// </summary>
protected override MemberInfo GetMemberInfo(IPropertyBase propertyBase)
=> propertyBase.GetMemberInfo(forMaterialization: false, forSet: false);
/// <summary>
/// This is an internal API that supports the Entity Framework Core infrastructure and not subject to
/// the same compatibility standards as public APIs. It may be changed or removed without notice in
/// any release. You should only use it directly in your code with extreme caution and knowing that
/// doing so can result in application failures when updating to a new Entity Framework Core release.
/// </summary>
public virtual void Create(
IPropertyBase propertyBase,
out Expression getterExpression,
out Expression hasSentinelExpression,
out Expression structuralGetterExpression,
out Expression hasStructuralSentinelExpression)
{
var boundMethod = GenericCreateExpressions.MakeGenericMethod(
propertyBase.DeclaringType.ContainingEntityType.ClrType,
propertyBase.DeclaringType.ClrType,
propertyBase.ClrType);
try
{
var parameters = new object?[] { GetMemberInfo(propertyBase), propertyBase, null, null, null, null };
boundMethod.Invoke(this, parameters);
getterExpression = (Expression)parameters[2]!;
hasSentinelExpression = (Expression)parameters[3]!;
structuralGetterExpression = (Expression)parameters[4]!;
hasStructuralSentinelExpression = (Expression)parameters[5]!;
}
catch (TargetInvocationException e) when (e.InnerException != null)
{
ExceptionDispatchInfo.Capture(e.InnerException).Throw();
throw;
}
}
private static readonly MethodInfo GenericCreateExpressions
= typeof(ClrPropertyGetterFactory).GetMethod(nameof(CreateExpressions), BindingFlags.Instance | BindingFlags.NonPublic)!;
private void CreateExpressions<TEntity, TStructuralType, TValue>(
MemberInfo memberInfo,
IPropertyBase? propertyBase,
out Expression<Func<TEntity, TValue>> getterExpression,
out Expression<Func<TEntity, bool>> hasSentinelExpression,
out Expression<Func<TStructuralType, TValue>> structuralGetterExpression,
out Expression<Func<TStructuralType, bool>> hasStructuralSentinelExpression)
{
var entityClrType = propertyBase?.DeclaringType.ContainingEntityType.ClrType ?? typeof(TEntity);
var propertyDeclaringType = propertyBase?.DeclaringType.ClrType ?? typeof(TEntity);
var entityParameter = Expression.Parameter(entityClrType, "entity");
var structuralParameter = Expression.Parameter(propertyDeclaringType, "instance");
var readExpression = CreateReadExpression(entityParameter, false);
var structuralReadExpression = CreateReadExpression(structuralParameter, true);
var hasSentinelValueExpression = readExpression.MakeHasSentinel(propertyBase);
var hasStructuralSentinelValueExpression = structuralReadExpression.MakeHasSentinel(propertyBase);
readExpression = ConvertReadExpression(readExpression, hasSentinelValueExpression);
structuralReadExpression = ConvertReadExpression(structuralReadExpression, hasStructuralSentinelValueExpression);
getterExpression = Expression.Lambda<Func<TEntity, TValue>>(readExpression, entityParameter);
hasSentinelExpression = Expression.Lambda<Func<TEntity, bool>>(hasSentinelValueExpression, entityParameter);
structuralGetterExpression = Expression.Lambda<Func<TStructuralType, TValue>>(structuralReadExpression, structuralParameter);
hasStructuralSentinelExpression =
Expression.Lambda<Func<TStructuralType, bool>>(hasStructuralSentinelValueExpression, structuralParameter);
Expression CreateReadExpression(ParameterExpression parameter, bool fromContainingType)
{
if (memberInfo.DeclaringType!.IsAssignableFrom(propertyDeclaringType))
{
return PropertyAccessorsFactory.CreateMemberAccess(propertyBase, parameter, memberInfo, fromContainingType);
}
// This path handles properties that exist only on proxy types and so only exist if the instance is a proxy
var converted = Expression.Variable(memberInfo.DeclaringType, "converted");
return Expression.Block(
new[] { converted },
new List<Expression>
{
Expression.Assign(
converted,
Expression.TypeAs(parameter, memberInfo.DeclaringType)),
Expression.Condition(
Expression.ReferenceEqual(converted, Expression.Constant(null)),
Expression.Default(memberInfo.GetMemberType()),
PropertyAccessorsFactory.CreateMemberAccess(propertyBase, converted, memberInfo, fromContainingType))
});
}
static Expression ConvertReadExpression(Expression expression, Expression sentinelExpression)
=> expression.Type != typeof(TValue)
? Expression.Condition(
sentinelExpression,
Expression.Constant(default(TValue), typeof(TValue)),
Expression.Convert(expression, typeof(TValue)))
: expression;
}
}