Search code examples
c#visual-studio-2013visual-studio-2017system.reflection

System.Reflection.TargetException: 'Non-static method requires a target.' only with VS 2017 not with VS 2013


I have been using the KendoDynamicLinq library for years on this project with no problems. I started to use VS 2017 Community Edition instead of VS 2013 on this same project/code and if I run the project with VS 2017 I get this exception: System.Reflection.TargetException: 'Non-static method requires a target.'

But if I run it with VS 2013, no problem, no exception and all is working.

I have been wracking my head around this but I cannot seem to find the answer. Any help or direction to point me to would be appreciate it.

private static MethodInfo GetMethod(string methodName, MethodInfo methodTypes, int genericArgumentsCount)
        {
            var methods = from method in typeof(Queryable).GetMethods(BindingFlags.Public | BindingFlags.Static)
                          let parameters = method.GetParameters()
                          let genericArguments = method.GetGenericArguments()
                          where method.Name == methodName &&
                            genericArguments.Length == genericArgumentsCount &&
                            parameters.Select(p => p.ParameterType).SequenceEqual((Type[])methodTypes.Invoke(null, genericArguments))
                          select method;
            return methods.FirstOrDefault();
        }

The line that is throwing is this:

parameters.Select(p => p.ParameterType).SequenceEqual((Type[])methodTypes.Invoke(null, genericArguments))

Do you see anything here that can work with VS2013 but not in VS 2017. No changes to the solution or the target framework.


Solution

  • Thank you mike z for pointing me to right direction. I am now checking if the methodTypes passed in is static or not and if it is static I am now passing in null as a target object. But if it is not static, then I create an instance of the object and pass that instead. Here is the new calling code:

    var m = ((Func<Type, Type[]>)this.GetType().GetMethod("SumAvgFunc", BindingFlags.Static | BindingFlags.NonPublic)
                            .MakeGenericMethod(proptype).Invoke(null, null)).GetMethodInfo();
                        object obj = null;
                        if (!m.IsStatic)
                        {
                            obj = Activator.CreateInstance(m.DeclaringType);
                        }
                        return GetMethod(aggregate, m, 1, obj).MakeGenericMethod(type);
    

    So I added a parameter to the GetMethod() method that takes an object which will be null if the method is static otherwise it will be an instance of the class whom method is a member. Here is GetMethod :

    private static MethodInfo GetMethod(string methodName, MethodInfo methodTypes, int genericArgumentsCount, object obj)
            {
                var methods = from method in typeof(Queryable).GetMethods(BindingFlags.Public | BindingFlags.Static)
                              let parameters = method.GetParameters()
                              let genericArguments = method.GetGenericArguments()
                              where method.Name == methodName &&
                                genericArguments.Length == genericArgumentsCount &&
                                parameters.Select(p => p.ParameterType).SequenceEqual((Type[])methodTypes.Invoke(obj, genericArguments))
                              select method;
                return methods.FirstOrDefault();
            }
    

    Now all works just fine as it was working before with VS 2013. Thank you mike z