5 // Copyright (c) 2007-2008 Jiri Moudry, Pascal Craponne
\r
7 // Permission is hereby granted, free of charge, to any person obtaining a copy
\r
8 // of this software and associated documentation files (the "Software"), to deal
\r
9 // in the Software without restriction, including without limitation the rights
\r
10 // to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
\r
11 // copies of the Software, and to permit persons to whom the Software is
\r
12 // furnished to do so, subject to the following conditions:
\r
14 // The above copyright notice and this permission notice shall be included in
\r
15 // all copies or substantial portions of the Software.
\r
17 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
\r
18 // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
\r
19 // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
\r
20 // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
\r
21 // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
\r
22 // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
\r
28 using System.Linq.Expressions;
\r
29 using System.Reflection;
\r
31 namespace DbLinq.Util
\r
34 /// What is extreme laziness? :)
\r
35 /// This class allows to get PropertyInfo or MethodInfo given a lambda (which allows to refactor members)
\r
40 static class ReflectionUtility
\r
42 public static MethodInfo GetMethodInfo(LambdaExpression lambdaExpression)
\r
44 var methodCallExpression = lambdaExpression.Body as MethodCallExpression;
\r
45 if (methodCallExpression != null)
\r
46 return methodCallExpression.Method;
\r
47 throw new ArgumentException("Lambda expression is not correctly formated for MethodInfo extraction");
\r
51 /// Returns a method info, providing a lambda
\r
53 /// <typeparam name="T"></typeparam>
\r
54 /// <param name="lambdaExpression"></param>
\r
55 /// <returns></returns>
\r
56 public static MethodInfo GetMethodInfo<T>(this Expression<Action<T>> lambdaExpression)
\r
58 return GetMethodInfo((LambdaExpression)lambdaExpression);
\r
62 /// Returns a method info, for example ReflectionUtility.GetMethodInfo(() => A.F()) would return "A.F()" MethodInfo
\r
64 /// <param name="lambdaExpression"></param>
\r
65 /// <returns></returns>
\r
66 public static MethodInfo GetMethodInfo(this Expression<Action> lambdaExpression)
\r
68 return GetMethodInfo((LambdaExpression)lambdaExpression);
\r
72 /// Returns MemberInfo specified in the lambda, optional parameter expression constraint.
\r
74 /// <param name="expression"></param>
\r
75 /// <returns></returns>
\r
76 public static MemberInfo GetMemberInfo(LambdaExpression expression)
\r
78 var paramExpr = expression.Parameters.Count == 1 ? expression.Parameters[0] : null;
\r
79 return GetMemberInfo(paramExpr, expression.Body);
\r
82 public static MemberInfo GetMemberCallInfo(LambdaExpression lambdaExpression)
\r
84 MethodCallExpression methodCallExpression = lambdaExpression.Body as MethodCallExpression;
\r
85 if (methodCallExpression != null)
\r
87 Expression memberExpression = methodCallExpression;
\r
90 switch (memberExpression.NodeType)
\r
92 case ExpressionType.MemberAccess:
\r
93 return ((MemberExpression)memberExpression).Member;
\r
94 case ExpressionType.Call:
\r
95 methodCallExpression = memberExpression as MethodCallExpression;
\r
96 memberExpression = (methodCallExpression.Object != null) ? methodCallExpression.Object : methodCallExpression.Arguments[0];
\r
104 return GetMemberInfo(lambdaExpression);
\r
108 /// Returns MemberInfo specified in the lambda, optional parameter expression constraint.
\r
110 /// <param name="lambdaExpression"></param>
\r
111 /// <returns></returns>
\r
112 private static MemberInfo GetMemberInfo(Expression optionalParamExpr, Expression expression)
\r
114 switch (expression.NodeType)
\r
116 // the ReflectionUtility Get** methods return the value as a object. If the value is a struct, we get a cast,
\r
117 // that we must unwrap
\r
118 case ExpressionType.Convert:
\r
119 case ExpressionType.ConvertChecked:
\r
120 return GetMemberInfo(optionalParamExpr, ((UnaryExpression)expression).Operand);
\r
121 case ExpressionType.MemberAccess:
\r
122 var me = (MemberExpression)expression;
\r
123 if (optionalParamExpr == null || me.Expression == optionalParamExpr)
\r
133 /// Returns a PropertyInfo, given a lambda
\r
134 /// For example: ReflectionUtility.GetPropertyInfo<A>(a => a.Prop) would return PropertyInfo "A.Prop"
\r
136 /// <typeparam name="T"></typeparam>
\r
137 /// <param name="lambdaExpression"></param>
\r
138 /// <returns></returns>
\r
139 public static PropertyInfo GetPropertyInfo<T>(this Expression<Func<T, object>> lambdaExpression)
\r
141 return (PropertyInfo)GetMemberInfo((LambdaExpression) lambdaExpression);
\r
145 /// Returns a PropertyInfo, given a lambda
\r
147 /// <param name="lambdaExpression"></param>
\r
148 /// <returns></returns>
\r
149 public static PropertyInfo GetPropertyInfo(this Expression<Func<object>> lambdaExpression)
\r
151 return (PropertyInfo)GetMemberInfo((LambdaExpression) lambdaExpression);
\r
155 /// Returns a MemberInfo, given a lambda
\r
156 /// For example: ReflectionUtility.GetMemberInfo<A>(a => a.Prop) would return MemberInfo "A.Prop"
\r
158 /// <typeparam name="T"></typeparam>
\r
159 /// <param name="lambdaExpression"></param>
\r
160 /// <returns></returns>
\r
161 public static MemberInfo GetMemberInfo<T>(this Expression<Func<T, object>> lambdaExpression)
\r
163 return GetMemberInfo((LambdaExpression) lambdaExpression);
\r
167 /// Returns a MemberInfo, given a lambda
\r
169 /// <param name="lambdaExpression"></param>
\r
170 /// <returns></returns>
\r
171 public static MemberInfo GetMemberInfo(this Expression<Func<object>> lambdaExpression)
\r
173 return GetMemberInfo((LambdaExpression) lambdaExpression);
\r