We read every piece of feedback, and take your input very seriously.
To see all available qualifiers, see our documentation.
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Implement a function that given an expression returns the path in the object like:
// Act string actual = ExpressionUtils.PathOf<ExpressionUtilsTests>(x => x.Recursion.Recursion.Recursion.Recursion); // Assert actual.ShouldBe("Recursion.Recursion.Recursion.Recursion");
C# implementation
public static string PathOf<T>(Expression<Func<T, object>> expression) { if (expression == null) { throw new ArgumentNullException(nameof(expression)); } Stack<string> memberNames = new Stack<string>(); MemberExpression memberExp = GetMemberExpression(expression.Body); while (memberExp != null) { memberNames.Push(memberExp.Member.Name); memberExp = GetMemberExpression(memberExp.Expression); } return string.Join(".", memberNames); } public static MemberExpression GetMemberExpression(Expression toUnwrap) { if (toUnwrap is UnaryExpression unaryExpression) { return unaryExpression.Operand as MemberExpression; } return toUnwrap as MemberExpression; }
The text was updated successfully, but these errors were encountered:
No branches or pull requests
Implement a function that given an expression returns the path in the object like:
C# implementation
The text was updated successfully, but these errors were encountered: