CSLA.NET 5.4.2
CSLA .NET is a software development framework that helps you build a reusable, maintainable object-oriented business layer for your app.
Reflect.cs
Go to the documentation of this file.
1//-----------------------------------------------------------------------
2// <copyright file="Reflect.cs" company="Marimer LLC">
3// Copyright (c) Marimer LLC. All rights reserved.
4// Website: https://cslanet.com
5// </copyright>
6// <summary>Provides strong-typed reflection of the <typeparamref name="TTarget"/> </summary>
7//-----------------------------------------------------------------------
8using System;
9using System.Linq.Expressions;
10using System.Reflection;
11
12namespace Csla.Reflection
13{
19 public static class Reflect<TTarget>
20 {
26 public static System.Reflection.MethodInfo GetMethod(Expression<Action<TTarget>> method)
27 {
28 return GetMethodInfo(method);
29 }
30
36 public static System.Reflection.MethodInfo GetMethod<T1>(Expression<Action<TTarget, T1>> method)
37 {
38 return GetMethodInfo(method);
39 }
40
46 public static System.Reflection.MethodInfo GetMethod<T1, T2>(Expression<Action<TTarget, T1, T2>> method)
47 {
48 return GetMethodInfo(method);
49 }
50
56 public static System.Reflection.MethodInfo GetMethod<T1, T2, T3>(Expression<Action<TTarget, T1, T2, T3>> method)
57 {
58 return GetMethodInfo(method);
59 }
60
61 private static System.Reflection.MethodInfo GetMethodInfo(Expression method)
62 {
63 if (method == null) throw new ArgumentNullException("method");
64
65 var lambda = method as LambdaExpression;
66 if (lambda == null) throw new ArgumentException("Not a lambda expression", "method");
67 if (lambda.Body.NodeType != ExpressionType.Call) throw new ArgumentException("Not a method call", "method");
68
69 return ((MethodCallExpression)lambda.Body).Method;
70 }
71
77 public static PropertyInfo GetProperty(Expression<Func<TTarget, object>> property)
78 {
79 var info = GetMemberInfo(property) as PropertyInfo;
80 if (info == null) throw new ArgumentException("Member is not a property");
81
82 return info;
83 }
84
93 public static PropertyInfo GetProperty<P>(Expression<Func<TTarget, P>> property)
94 {
95 var info = GetMemberInfo(property) as PropertyInfo;
96 if (info == null) throw new ArgumentException("Member is not a property");
97
98 return info;
99 }
100
106 public static FieldInfo GetField(Expression<Func<TTarget, object>> field)
107 {
108 var info = GetMemberInfo(field) as FieldInfo;
109 if (info == null) throw new ArgumentException("Member is not a field");
110
111 return info;
112 }
113
114 private static MemberInfo GetMemberInfo(Expression member)
115 {
116 if (member == null) throw new ArgumentNullException("member");
117
118 var lambda = member as LambdaExpression;
119 if (lambda == null) throw new ArgumentException("Not a lambda expression", "member");
120
121 MemberExpression memberExpr = null;
122
123 // The Func<TTarget, object> we use returns an object, so first statement can be either
124 // a cast (if the field/property does not return an object) or the direct member access.
125 if (lambda.Body.NodeType == ExpressionType.Convert)
126 {
127 // The cast is an unary expression, where the operand is the
128 // actual member access expression.
129 memberExpr = ((UnaryExpression)lambda.Body).Operand as MemberExpression;
130 }
131 else if (lambda.Body.NodeType == ExpressionType.MemberAccess)
132 {
133 memberExpr = lambda.Body as MemberExpression;
134 }
135
136 if (memberExpr == null) throw new ArgumentException("Not a member access", "member");
137
138 return memberExpr.Member;
139 }
140 }
141}