c# - Enumerable.Contains 与 MethodInfo

标签 c# reflection linq-expressions

有这个问题。基本上我使用 System.Linq.Expressions 来建立一个动态
Linq 和 EF 表达式。

我想传入 IEnumerable 并建立一个包含。我无法从 IEnumerable 中获取方法信息。

这是有效的,因为没有过载

MethodInfo contains = typeof(List<int>).GetMethod("Contains");

我无法从 IEnumerable 中获取方法信息。

这是完整的代码。 (第一行是我试图使用 Ienumerable 找出的内容)
MethodInfo contains = typeof(List<int>).GetMethod("Contains");
var thisListExpressionParameter = Expression.Constant(lst, typeof(IEnumerable<Z>));
var ContainsMethodCall = Expression.Call(thisListExpressionParameter, ContainsOffOfIEnumerable, RightHandSide.thisMemberExpression);

 return Expression.Lambda<Func<T, bool>>(ContainsMethodCall, RightHandSide.thisParameterExpression);

最佳答案

这是答案,

  MethodInfo ContainsOffOfIEnumerable = OverloadedMethodFinder.FindOverloadedMethodToCall("Contains", typeof(Enumerable), typeof(IEnumerable<Z>), typeof(Z))
                                              .MakeGenericMethod(typeof(Z));

        //grab the lst and make it a constants
        ConstantExpression ConstantListExpressionParameter = Expression.Constant(ListToCheck, typeof(IEnumerable<Z>));

        //this is an extension method so you pass in the list too
        //now create the call...basically contains(myList,int to check for)
        MethodCallExpression ContainsMethodCall = Expression.Call(ContainsOffOfIEnumerable, ConstantListExpressionParameter, RightHandSide.PropertyMemberExpression);

        //return the expression now
        return Expression.Lambda<Func<T, bool>>(ContainsMethodCall, RightHandSide.ParametersForExpression);

OverloadedMethodFinder 在下面
   /// <summary>
/// Helps find an overloaded method
/// </summary>
public static class OverloadedMethodFinder
{

    #region Public Static Methods

    /// <summary>
    /// Attempts to find the overloaded method that we want to call. Returns null if not found. This overload looks at the parameter types passed in vs method parameters off of the type we pass in
    /// </summary>
    /// <param name="MethodNameToRetrieve">What is the method to name to find</param>
    /// <param name="TypeToLookThroughTheMethods">The type to retrieve the methods off of, so we can look through it and try to find the correct method</param>
    /// <param name="MethodParameterTypes">Look for the method parameter types in the method to match. If the method takes a string and an int, then we will look for that in every method</param>
    /// <returns>Method info found, or null value if not found</returns>
    public static MethodInfo FindOverloadedMethodToCall(string MethodNameToRetrieve, Type TypeToLookThroughTheMethods, params Type[] MethodParameterTypes)
    {
        //going to use the overload. So we can create the func with calling the other method
        return FindOverloadedMethodToCall(MethodNameToRetrieve, TypeToLookThroughTheMethods, x => MethodParameterSelector(x, MethodParameterTypes));
    }

    /// <summary>
    /// Attempts to find the overloaded method that we want to call. Returns null if not found. This method will try to evaluate the MethodSelect for each method and check to see if it returns true.
    /// </summary>
    /// <param name="MethodNameToRetrieve">What is the method to name to find</param>
    /// <param name="MethodSelector">Gives the calling method the ability to look through the parameters and pick the correct method</param>
    /// <param name="TypeToLookThroughTheMethods">The type to retrieve the methods off of, so we can look through it and try to find the correct method</param>
    /// <returns>Method info found, or null value if not found</returns>
    public static MethodInfo FindOverloadedMethodToCall(string MethodNameToRetrieve, Type TypeToLookThroughTheMethods, Func<MethodInfo, bool> MethodSelector)
    {
        //use the overload
        return FindOverloadedMethodToCall(MethodNameToRetrieve, TypeToLookThroughTheMethods.GetMethods(), MethodSelector);
    }

    /// <summary>
    /// Attempts to find the overloaded method that we want to call. Returns null if not found. This method will try to evaluate the MethodSelect for each method and check to see if it returns true.
    /// Call this method if you already have the method info's that match the same name you are looking for
    /// </summary>
    /// <param name="MethodNameToRetrieve">What is the method to name to find</param>
    /// <param name="MethodSelector">Gives the calling method the ability to look through the parameters and pick the correct method</param>
    /// <param name="MethodsToLookThrough">Methods that have the same name. Or methods to loop through and inspect against the method selector.</param>
    /// <returns>Method info found, or null value if not found</returns>
    public static MethodInfo FindOverloadedMethodToCall(string MethodNameToRetrieve, IEnumerable<MethodInfo> MethodsToLookThrough, Func<MethodInfo, bool> MethodSelector)
    {
        //let's start looping through the methods to see if we can find a match
        foreach (MethodInfo MethodToInspect in MethodsToLookThrough)
        {
            //is it the method name? and does it match the method selector passed in?
            if (string.Equals(MethodNameToRetrieve, MethodToInspect.Name, StringComparison.OrdinalIgnoreCase) && MethodSelector(MethodToInspect))
            {
                //we have a match...return the method now
                return MethodToInspect;
            }
        }

        //we never found a match, so just return null
        return null;
    }

    #endregion

    #region Private Static Methods

    /// <summary>
    /// Private helper method to look at the current method and inspect it for the method parameter types. If they match return true, else return false
    /// </summary>
    /// <param name="MethodToEvaluate">Method to evaluate and check if we have a match based on the method parameter types</param>
    /// <param name="MethodParameterTypes"></param>
    /// <returns>Do we have a match? Do the method parameter types match?</returns>
    private static bool MethodParameterSelector(MethodInfo MethodToEvaluate, params Type[] MethodParameterTypes)
    {
        //we are going to match the GetParameters and the MethodParameterTypes. It needs to match index for index and type for type. So GetParameters[0].Type must match MethodParameterTypes[0].Type...[1].Type must match [1].Type

        //holds the index with the method parameter types we are up too
        int i = 0;

        //let's loop through the parameters
        foreach (ParameterInfo thisParameter in MethodToEvaluate.GetParameters())
        {
            //it's a generic parameter...ie...TSource then we are going to ignore it because whatever we pass in would be TSource
            if (!thisParameter.ParameterType.IsGenericParameter)
            {
                //is this a generic type? we need to compare this differently
                if (thisParameter.ParameterType.IsGenericType)
                {
                    //is the method parameter a generic type?
                    if (!MethodParameterTypes[i].IsGenericType)
                    {
                        //it isn't so return false..cause they aren't the same
                        return false;
                    }

                    //if the generic type's don't match then return false...This might be problematic...it works for the scenario which I'm using it for so we will leave this and modify afterwards
                    if (thisParameter.ParameterType.GetGenericTypeDefinition() != MethodParameterTypes[i].GetGenericTypeDefinition())
                    {
                        //doesn't match return false
                        return false;
                    }
                }
                else if (thisParameter.ParameterType != MethodParameterTypes[i].UnderlyingSystemType)
                {
                    //this is a regular parameter so we can compare it normally
                    //we don't have a match...so return false
                    return false;
                }
            }

            //increment the index
            i++;
        }

        //if we get here then everything matches so return true
        return true;
    }

    #endregion

}

关于c# - Enumerable.Contains 与 MethodInfo,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17634908/

相关文章:

c# - 从 Web Api Controller 返回 http 状态代码

c# - 如何正确转换通过反射创建的对象

c# - .Net 程序集中的循环引用

java - 如何查找java bean中包含的所有成员变量的字段

c# - 使用 foreach 循环构建自定义谓词以充当过滤器

c# - 具有 null 属性的嵌套属性的动态 linq 排序

c# - 从 FilterQueryOption 获取 Linq 表达式抛出 CLR 异常

c# - 添加子菜单删除快捷键

c# - 如何在Windows 8应用程序中创建metroLog应用程序?

c# - Moq & C# : Invalid callback. 带参数的方法设置无法调用带参数的回调