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
83 /// Returns MemberInfo specified in the lambda, optional parameter expression constraint.
\r
85 /// <param name="lambdaExpression"></param>
\r
86 /// <returns></returns>
\r
87 private static MemberInfo GetMemberInfo(Expression optionalParamExpr, Expression expression)
\r
89 switch (expression.NodeType)
\r
91 // the ReflectionUtility Get** methods return the value as a object. If the value is a struct, we get a cast,
\r
92 // that we must unwrap
\r
93 case ExpressionType.Convert:
\r
94 case ExpressionType.ConvertChecked:
\r
95 return GetMemberInfo(optionalParamExpr, ((UnaryExpression)expression).Operand);
\r
96 case ExpressionType.MemberAccess:
\r
97 var me = (MemberExpression)expression;
\r
98 if (optionalParamExpr == null || me.Expression == optionalParamExpr)
\r
108 /// Returns a PropertyInfo, given a lambda
\r
109 /// For example: ReflectionUtility.GetPropertyInfo<A>(a => a.Prop) would return PropertyInfo "A.Prop"
\r
111 /// <typeparam name="T"></typeparam>
\r
112 /// <param name="lambdaExpression"></param>
\r
113 /// <returns></returns>
\r
114 public static PropertyInfo GetPropertyInfo<T>(this Expression<Func<T, object>> lambdaExpression)
\r
116 return (PropertyInfo)GetMemberInfo((LambdaExpression) lambdaExpression);
\r
120 /// Returns a PropertyInfo, given a lambda
\r
122 /// <param name="lambdaExpression"></param>
\r
123 /// <returns></returns>
\r
124 public static PropertyInfo GetPropertyInfo(this Expression<Func<object>> lambdaExpression)
\r
126 return (PropertyInfo)GetMemberInfo((LambdaExpression) lambdaExpression);
\r
130 /// Returns a MemberInfo, given a lambda
\r
131 /// For example: ReflectionUtility.GetMemberInfo<A>(a => a.Prop) would return MemberInfo "A.Prop"
\r
133 /// <typeparam name="T"></typeparam>
\r
134 /// <param name="lambdaExpression"></param>
\r
135 /// <returns></returns>
\r
136 public static MemberInfo GetMemberInfo<T>(this Expression<Func<T, object>> lambdaExpression)
\r
138 return GetMemberInfo((LambdaExpression) lambdaExpression);
\r
142 /// Returns a MemberInfo, given a lambda
\r
144 /// <param name="lambdaExpression"></param>
\r
145 /// <returns></returns>
\r
146 public static MemberInfo GetMemberInfo(this Expression<Func<object>> lambdaExpression)
\r
148 return GetMemberInfo((LambdaExpression) lambdaExpression);
\r