forked from mongodb/mongo-csharp-driver
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathReflectionInfo.cs
More file actions
133 lines (103 loc) · 5.94 KB
/
ReflectionInfo.cs
File metadata and controls
133 lines (103 loc) · 5.94 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
/* Copyright 2010-present MongoDB Inc.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
using System;
using System.Linq.Expressions;
using System.Reflection;
namespace MongoDB.Driver.Linq.Linq3Implementation.Reflection
{
internal static class ReflectionInfo
{
public static ConstructorInfo Constructor<TObject>(Expression<Func<TObject>> lambda) =>
ExtractConstructorInfoFromLambda(lambda);
public static ConstructorInfo Constructor<T1, TObject>(Expression<Func<T1, TObject>> lambda) =>
ExtractConstructorInfoFromLambda(lambda);
public static ConstructorInfo Constructor<T1, T2, TObject>(Expression<Func<T1, T2, TObject>> lambda) =>
ExtractConstructorInfoFromLambda(lambda);
public static ConstructorInfo Constructor<T1, T2, T3, TObject>(Expression<Func<T1, T2, T3, TObject>> lambda) =>
ExtractConstructorInfoFromLambda(lambda);
public static ConstructorInfo Constructor<T1, T2, T3, T4, TObject>(Expression<Func<T1, T2, T3, T4, TObject>> lambda) =>
ExtractConstructorInfoFromLambda(lambda);
public static ConstructorInfo Constructor<T1, T2, T3, T4, T5, TObject>(Expression<Func<T1, T2, T3, T4, T5, TObject>> lambda) =>
ExtractConstructorInfoFromLambda(lambda);
public static ConstructorInfo Constructor<T1, T2, T3, T4, T5, T6, TObject>(Expression<Func<T1, T2, T3, T4, T5, T6, TObject>> lambda) =>
ExtractConstructorInfoFromLambda(lambda);
public static ConstructorInfo Constructor<T1, T2, T3, T4, T5, T6, T7, TObject>(Expression<Func<T1, T2, T3, T4, T5, T6, T7, TObject>> lambda) =>
ExtractConstructorInfoFromLambda(lambda);
public static MethodInfo Method<TResult>(Expression<Func<TResult>> lambda) =>
ExtractMethodInfoFromLambda(lambda);
public static MethodInfo Method<T1, TResult>(Expression<Func<T1, TResult>> lambda) =>
ExtractMethodInfoFromLambda(lambda);
public static MethodInfo Method<T1, T2, TResult>(Expression<Func<T1, T2, TResult>> lambda) =>
ExtractMethodInfoFromLambda(lambda);
public static MethodInfo Method<T1, T2, T3, TResult>(Expression<Func<T1, T2, T3, TResult>> lambda) =>
ExtractMethodInfoFromLambda(lambda);
public static MethodInfo Method<T1, T2, T3, T4, TResult>(Expression<Func<T1, T2, T3, T4, TResult>> lambda) =>
ExtractMethodInfoFromLambda(lambda);
public static MethodInfo Method<T1, T2, T3, T4, T5, TResult>(Expression<Func<T1, T2, T3, T4, T5, TResult>> lambda) => ExtractMethodInfoFromLambda(lambda);
public static MethodInfo Method<T1, T2, T3, T4, T5, T6, TResult>(Expression<Func<T1, T2, T3, T4, T5, T6, TResult>> lambda) =>
ExtractMethodInfoFromLambda(lambda);
public static MethodInfo Method<T1, T2, T3, T4, T5, T6, T7, TResult>(Expression<Func<T1, T2, T3, T4, T5, T6, T7, TResult>> lambda) =>
ExtractMethodInfoFromLambda(lambda);
public static MethodInfo Method<T1, T2, T3, T4, T5, T6, T7, T8, TResult>(Expression<Func<T1, T2, T3, T4, T5, T6, T7, T8, TResult>> lambda) =>
ExtractMethodInfoFromLambda(lambda);
public static PropertyInfo Property<TObject, TProperty>(Expression<Func<TObject, TProperty>> lambda) =>
ExtractPropertyInfoFromLambda(lambda);
public static MethodInfo IndexerGet<TObject, TIndex, TValue>(Expression<Func<TObject, TIndex, TValue>> lambda) =>
ExtractMethodInfoFromLambda(lambda);
public static MethodInfo IndexerSet<TObject, TIndex, TValue>(Expression<Func<TObject, TIndex, TValue>> lambda) =>
ExtractIndexerSetMethodInfoFromLambda(lambda);
// private static methods
private static ConstructorInfo ExtractConstructorInfoFromLambda(LambdaExpression lambda)
{
var newExpression = (NewExpression)lambda.Body;
return newExpression.Constructor;
}
private static MethodInfo ExtractIndexerSetMethodInfoFromLambda(LambdaExpression lambda)
{
var getMethod = ExtractMethodInfoFromLambda(lambda);
var declaringType = getMethod.DeclaringType;
foreach (var propertyInfo in declaringType.GetProperties())
{
if (propertyInfo.GetGetMethod() == getMethod)
{
return propertyInfo.GetSetMethod();
}
}
throw new ArgumentException($"No set method found for: {lambda.Body}.", nameof(lambda));
}
private static MethodInfo ExtractMethodInfoFromLambda(LambdaExpression lambda)
{
var methodCallExpression = (MethodCallExpression)lambda.Body;
var method = methodCallExpression.Method;
if (method.IsGenericMethod)
{
method = method.GetGenericMethodDefinition();
}
return method;
}
public static PropertyInfo ExtractPropertyInfoFromLambda(LambdaExpression lambda)
{
var propertyExpression = (MemberExpression)lambda.Body;
var propertyInfo = (PropertyInfo)propertyExpression.Member;
var declaringType = propertyInfo.DeclaringType;
if (declaringType.IsConstructedGenericType)
{
var declaringTypeDefinition = declaringType.GetGenericTypeDefinition();
propertyInfo = declaringTypeDefinition.GetProperty(propertyInfo.Name);
}
return propertyInfo;
}
}
}