using System;
using System.Linq.Expressions;
using System.Reflection;
namespace MessagePack.Internal
{
public static class ExpressionUtility
{
// Method
static MethodInfo GetMethodInfoCore(LambdaExpression expression)
{
if (expression == null)
{
throw new ArgumentNullException("expression");
}
return (expression.Body as MethodCallExpression).Method;
}
///
/// Get MethodInfo from Expression for Static(with result) method.
///
public static MethodInfo GetMethodInfo(Expression> expression)
{
return GetMethodInfoCore(expression);
}
///
/// Get MethodInfo from Expression for Static(void) method.
///
public static MethodInfo GetMethodInfo(Expression expression)
{
return GetMethodInfoCore(expression);
}
///
/// Get MethodInfo from Expression for Instance(with result) method.
///
public static MethodInfo GetMethodInfo(Expression> expression)
{
return GetMethodInfoCore(expression);
}
///
/// Get MethodInfo from Expression for Instance(void) method.
///
public static MethodInfo GetMethodInfo(Expression> expression)
{
return GetMethodInfoCore(expression);
}
// WithArgument(for ref, out) helper
///
/// Get MethodInfo from Expression for Instance(with result) method.
///
public static MethodInfo GetMethodInfo(Expression> expression)
{
return GetMethodInfoCore(expression);
}
// Property
static MemberInfo GetMemberInfoCore(Expression source)
{
if (source == null)
{
throw new ArgumentNullException("source");
}
var memberExpression = source.Body as MemberExpression;
return memberExpression.Member;
}
public static PropertyInfo GetPropertyInfo(Expression> expression)
{
return GetMemberInfoCore(expression) as PropertyInfo;
}
// Field
public static FieldInfo GetFieldInfo(Expression> expression)
{
return GetMemberInfoCore(expression) as FieldInfo;
}
}
}