<commit_before>﻿using System;
using System.Linq.Expressions;
using System.Reflection;

namespace Kirkin.Linq.Expressions
{
    /// <summary>
    /// Common utility methods for processing expression trees.
    /// </summary>
    public static class ExpressionUtil
    {
        #region Member

        /// <summary>
        /// Resolves the MemberInfo from the given member expression.
        /// </summary>
        public static MemberInfo Member<T, TMember>(Expression<Func<T, TMember>> expr)
        {
            if (expr == null) throw new ArgumentNullException(nameof(expr));

            return MemberFromExpression(expr.Body);
        }

        /// <summary>
        /// Resolves the MemberInfo from the given member expression.
        /// </summary>
        public static MemberInfo Member<TMember>(Expression<Func<TMember>> expr)
        {
            if (expr == null) throw new ArgumentNullException(nameof(expr));

            return MemberFromExpression(expr.Body);
        }

        /// <summary>
        /// Resolves the MemberInfo from the given member expression.
        /// </summary>
        public static MemberInfo Member<T>(Expression<Func<T, object>> expr)
        {
            if (expr == null) throw new ArgumentNullException(nameof(expr));

            UnaryExpression convertExpr = expr.Body as UnaryExpression;

            return convertExpr == null
                ? MemberFromExpression(expr.Body)
                // Assuming struct return type. Member expression will be
                // wrapped by Expression.Convert(<memberExpr>, typeof(object)).
                : MemberFromExpression(convertExpr.Operand);
        }

        /// <summary>
        /// Resolves the MemberInfo from the given member expression.
        /// </summary>
        /// <remarks>
        /// Needed for when the Func return type is unknown.
        /// </remarks>
        public static MemberInfo Member(Expression<Func<object>> expr)
        {
            if (expr == null) throw new ArgumentNullException(nameof(expr));

            UnaryExpression convertExpr = expr.Body as UnaryExpression;

            return convertExpr == null
                ? MemberFromExpression(expr.Body)
                // Assuming struct return type. Member expression will be
                // wrapped by Expression.Convert(<memberExpr>, typeof(object)).
                : MemberFromExpression(convertExpr.Operand);
        }

        /// <summary>
        /// Casts the given expression to <see cref="MemberExpression"/>
        /// and returns its <see cref="MemberExpression.Member" />.
        /// </summary>
        private static MemberInfo MemberFromExpression(Expression expr)
        {
            MemberExpression memberExpression = expr as MemberExpression;

            if (memberExpression == null) {
                throw new InvalidOperationException("The expression is not a MemberExpression.");
            }

            return memberExpression.Member;
        }

        #endregion

        #region MemberName

        /// <summary>
        /// Resolves the name of the member specified by the given member expression.
        /// </summary>
        [Obsolete("Use Member instead.")]
        public static string MemberName<T, TMember>(Expression<Func<T, TMember>> expr)
        {
            return Member(expr).Name;
        }

        /// <summary>
        /// Resolves the name of the member specified by the given member expression.
        /// </summary>
        [Obsolete("Use Member instead.")]
        public static string MemberName<TMember>(Expression<Func<TMember>> expr)
        {
            return Member(expr).Name;
        }

        /// <summary>
        /// Resolves the name of the member specified by the given member expression.
        /// </summary>
        [Obsolete("Use Member instead.")]
        public static string MemberName<T>(Expression<Func<T, object>> expr)
        {
            return Member(expr).Name;
        }

        /// <summary>
        /// Resolves the name of the member specified by the given member expression.
        /// </summary>
        /// <remarks>
        /// Needed for when the Func return type is unknown.
        /// </remarks>
        [Obsolete("Use Member instead.")]
        public static string MemberName(Expression<Func<object>> expr)
        {
            return Member(expr).Name;
        }

        #endregion

        #region Method

        /// <summary>
        /// Resolves the <see cref="MethodInfo"/> from the given method call expression.
        /// </summary>
        public static MethodInfo Method<T>(Expression<Action<T>> expr)<commit_msg>ExpressionUtil: ALLOW_MEMBER_NAME switch<commit_after>﻿using System;
using System.Linq.Expressions;
using System.Reflection;

namespace Kirkin.Linq.Expressions
{
    /// <summary>
    /// Common utility methods for processing expression trees.
    /// </summary>
    public static class ExpressionUtil
    {
        #region Member

        /// <summary>
        /// Resolves the MemberInfo from the given member expression.
        /// </summary>
        public static MemberInfo Member<T, TMember>(Expression<Func<T, TMember>> expr)
        {
            if (expr == null) throw new ArgumentNullException(nameof(expr));

            return MemberFromExpression(expr.Body);
        }

        /// <summary>
        /// Resolves the MemberInfo from the given member expression.
        /// </summary>
        public static MemberInfo Member<TMember>(Expression<Func<TMember>> expr)
        {
            if (expr == null) throw new ArgumentNullException(nameof(expr));

            return MemberFromExpression(expr.Body);
        }

        /// <summary>
        /// Resolves the MemberInfo from the given member expression.
        /// </summary>
        public static MemberInfo Member<T>(Expression<Func<T, object>> expr)
        {
            if (expr == null) throw new ArgumentNullException(nameof(expr));

            UnaryExpression convertExpr = expr.Body as UnaryExpression;

            return convertExpr == null
                ? MemberFromExpression(expr.Body)
                // Assuming struct return type. Member expression will be
                // wrapped by Expression.Convert(<memberExpr>, typeof(object)).
                : MemberFromExpression(convertExpr.Operand);
        }

        /// <summary>
        /// Resolves the MemberInfo from the given member expression.
        /// </summary>
        /// <remarks>
        /// Needed for when the Func return type is unknown.
        /// </remarks>
        public static MemberInfo Member(Expression<Func<object>> expr)
        {
            if (expr == null) throw new ArgumentNullException(nameof(expr));

            UnaryExpression convertExpr = expr.Body as UnaryExpression;

            return convertExpr == null
                ? MemberFromExpression(expr.Body)
                // Assuming struct return type. Member expression will be
                // wrapped by Expression.Convert(<memberExpr>, typeof(object)).
                : MemberFromExpression(convertExpr.Operand);
        }

        /// <summary>
        /// Casts the given expression to <see cref="MemberExpression"/>
        /// and returns its <see cref="MemberExpression.Member" />.
        /// </summary>
        private static MemberInfo MemberFromExpression(Expression expr)
        {
            MemberExpression memberExpression = expr as MemberExpression;

            if (memberExpression == null) {
                throw new InvalidOperationException("The expression is not a MemberExpression.");
            }

            return memberExpression.Member;
        }

        #endregion

        #region MemberName

        /// <summary>
        /// Resolves the name of the member specified by the given member expression.
        /// </summary>
#if !ALLOW_MEMBER_NAME
        [Obsolete("Use Member instead.")]
#endif
        public static string MemberName<T, TMember>(Expression<Func<T, TMember>> expr)
        {
            return Member(expr).Name;
        }

        /// <summary>
        /// Resolves the name of the member specified by the given member expression.
        /// </summary>
#if !ALLOW_MEMBER_NAME
        [Obsolete("Use Member instead.")]
#endif
        public static string MemberName<TMember>(Expression<Func<TMember>> expr)
        {
            return Member(expr).Name;
        }

        /// <summary>
        /// Resolves the name of the member specified by the given member expression.
        /// </summary>
#if !ALLOW_MEMBER_NAME
        [Obsolete("Use Member instead.")]
#endif
        public static string MemberName<T>(Expression<Func<T, object>> expr)
        {
            return Member(expr).Name;
        }

        /// <summary>
        /// Resolves the name of the member specified by the given member expression.
        /// </summary>
        /// <remarks>
        /// Needed for when the Func return type is unknown.
        /// </remarks>
#if !ALLOW_MEMBER_NAME
        [Obsolete("Use Member instead.")]
#endif
        public static string MemberName(Expression<Func<object>> expr)
        {
            return Member(expr).Name;
        }

        #endregion

        #region Method

        /// <summary>
        /// Resolves the <see cref="MethodInfo"/> from the given method call expression.
        /// </summary>
        public static MethodInfo Method<T>(Expression<Action<T>> expr)
{'fraction_non_alphanumeric': 0.10529106139327749, 'fraction_numerical': 0.0, 'mean_word_length': 1.9319241529375195, 'pattern_counts': {'":': 0, '<': 115, '<?xml version=': 0, '>': 115, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '1552607', 'n_tokens_mistral': 2271, 'n_tokens_neox': 2219, 'n_words': 691}