Skip to content

Instantly share code, notes, and snippets.

@werwolfby
Last active December 24, 2017 09:42
Show Gist options
  • Save werwolfby/aeabc8b4a26ead4b4efc2bcd5b718e1a to your computer and use it in GitHub Desktop.
Save werwolfby/aeabc8b4a26ead4b4efc2bcd5b718e1a to your computer and use it in GitHub Desktop.
public static class StaticReflection
{
public static MemberExpression GetMemberExpression(LambdaExpression expression)
{
return GetMemberExpression() ?? throw new ArgumentException(@"Not a member access", nameof(expression));
MemberExpression GetMemberExpression()
{
switch (expression.Body.NodeType)
{
case ExpressionType.Convert:
return ((UnaryExpression)expression.Body).Operand as MemberExpression;
case ExpressionType.MemberAccess:
return expression.Body as MemberExpression;
default:
return null;
}
}
}
public static PropertyInfo GetProperty<TProperty>(Expression<Func<TProperty>> expression)
=> (PropertyInfo)GetMember(expression);
public static MethodInfo GetMethod<TProperty>(Expression<Func<TProperty>> expression)
=> ((MethodCallExpression)expression.Body).Method;
public static MethodInfo GetMethod(Expression<Action> expression)
=> ((MethodCallExpression)expression.Body).Method;
public static FieldInfo GetField<TResult>(Expression<Func<TResult>> expression)
=> (FieldInfo)GetMember(expression);
public static MemberInfo GetMember<TProperty>(Expression<Func<TProperty>> expression)
=> GetMemberExpression(expression).Member;
public static string GetPropertyName<TProperty>(Expression<Func<TProperty>> expression)
=> GetProperty(expression).Name;
public static string GetPropertyPath<TProperty>(Expression<Func<TProperty>> expression)
=> string.Join(".", GetPropertyPathItems(expression));
public static string[] GetPropertyPathItems<TProperty>(Expression<Func<TProperty>> expression)
{
var path = new List<string>();
var currentExpression = GetMemberExpression(expression);
path.Add(currentExpression.Member.Name);
while (!(currentExpression.Expression is ParameterExpression))
{
currentExpression = (MemberExpression)currentExpression.Expression;
path.Insert(0, currentExpression.Member.Name);
}
return path.ToArray();
}
}
public static class StaticReflection<TEntity>
{
public static PropertyInfo GetProperty<TProperty>(Expression<Func<TEntity, TProperty>> expression)
=> (PropertyInfo)GetMember(expression);
public static MethodInfo GetMethod<TProperty>(Expression<Func<TEntity, TProperty>> expression)
=> ((MethodCallExpression)expression.Body).Method;
public static MethodInfo GetMethod(Expression<Action<TEntity>> expression)
=> ((MethodCallExpression)expression.Body).Method;
public static FieldInfo GetField<TResult>(Expression<Func<TEntity, TResult>> expression)
=> (FieldInfo)GetMember(expression);
public static MemberInfo GetMember<TProperty>(Expression<Func<TEntity, TProperty>> expression)
=> StaticReflection.GetMemberExpression(expression).Member;
public static string GetPropertyName<TProperty>(Expression<Func<TEntity, TProperty>> expression)
=> GetProperty(expression).Name;
public static string GetPropertyPath<TProperty>(Expression<Func<TEntity, TProperty>> expression)
=> string.Join(".", GetPropertyPathItems(expression));
public static string[] GetPropertyPathItems<TProperty>(Expression<Func<TEntity, TProperty>> expression)
{
var path = new List<string>();
var currentExpression = StaticReflection.GetMemberExpression(expression);
path.Add(currentExpression.Member.Name);
while (!(currentExpression.Expression is ParameterExpression))
{
currentExpression = (MemberExpression)currentExpression.Expression;
path.Insert(0, currentExpression.Member.Name);
}
return path.ToArray();
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment