Skip to content

Commit

Permalink
Added new "GetMethodInfo" and "GetPropertyInfo" methods to the "Refle…
Browse files Browse the repository at this point in the history
…ctionUtils" class

The methods allows getting either method info or property info of an expression.
  • Loading branch information
abjerner committed Sep 11, 2023
1 parent fba75f7 commit f12eb5f
Show file tree
Hide file tree
Showing 2 changed files with 86 additions and 0 deletions.
39 changes: 39 additions & 0 deletions src/Skybrud.Essentials/Reflection/ReflectionUtils.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
using System.Diagnostics;
using System.Diagnostics.CodeAnalysis;
using System.Linq;
using System.Linq.Expressions;
using System.Reflection;
using System.Runtime.CompilerServices;
using Skybrud.Essentials.Collections;
Expand Down Expand Up @@ -542,6 +543,44 @@ public static bool IsExtensionMethod(MethodInfo? method) {

#endif

/// <summary>
/// Returns the <see cref="MethodInfo"/> identified by the specified <paramref name="expression"/>.
/// </summary>
/// <typeparam name="T">The type of the expression.</typeparam>
/// <param name="expression">The expression.</param>
/// <returns>An instance of <see cref="MethodInfo"/>.</returns>
/// <exception cref="ArgumentException">If <paramref name="expression"/> is not a method expression.</exception>
public static MethodInfo GetMethodInfo<T>(Expression<T> expression) {

if (expression.Body is not MethodCallExpression methodExpression) {
throw new ArgumentException($"Expression body is not of type MethodCallExpression: {expression}");
}

return methodExpression.Method;

}

/// <summary>
/// Returns the <see cref="PropertyInfo"/> identified by the specified <paramref name="expression"/>.
/// </summary>
/// <typeparam name="T">The type of the expression.</typeparam>
/// <param name="expression">The expression.</param>
/// <returns>An instance of <see cref="PropertyInfo"/>.</returns>
/// <exception cref="ArgumentException">If <paramref name="expression"/> is not a property expression.</exception>
public static PropertyInfo GetPropertyInfo<T>(Expression<T> expression) {

if (expression.Body is not MemberExpression member) {
throw new ArgumentException($"Expression body is not of type MemberExpression: {expression}");
}

if (member.Member is not PropertyInfo propertyInfo) {
throw new ArgumentException($"Expression member is not a property: {expression}");
}

return propertyInfo;

}

}

}
47 changes: 47 additions & 0 deletions src/TestProject1/ReflectionTests.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
using System.Linq.Expressions;
using Skybrud.Essentials.Reflection;

namespace TestProject1 {

[TestClass]
public class ReflectionTests {

[TestMethod]
public void GetMethodInfo() {

Expression<Func<MyModel, string>> expression = GetExpression<MyModel, string>(x => x.MyMethod());

var method = ReflectionUtils.GetMethodInfo(expression);

Assert.AreEqual("MyMethod", method.Name);

}

[TestMethod]
public void GetPropertyInfo() {

Expression<Func<MyModel, string>> expression = GetExpression<MyModel, string>(x => x.MyProperty);

var property = ReflectionUtils.GetPropertyInfo(expression);

Assert.AreEqual("MyProperty", property.Name);

}

private Expression<Func<TModel, TProperty>> GetExpression<TModel, TProperty>(Expression<Func<TModel, TProperty>> expression) {
return expression;
}

public class MyModel {

public string MyProperty { get; set; } = "Hello There";

public string MyMethod() {
return "Hello Where";
}

}

}

}

0 comments on commit f12eb5f

Please sign in to comment.