jeudi 3 novembre 2022

MakeGenericMethod For Max Is Not Working In .Net 6

So we recently migrated our project from .net framework 4.7.2 to .net 6 and we are testing the code for any issues , while testing i found out that the following method for Max is not working in .net 6

var maxOfStringMethod = (from method in typeof(Enumerable).GetMethods()
                                   where method.Name == "Max"
                        && method.GetParameters().Length == 2 &&
                        method.GetParameters()[1].ParameterType.GenericTypeArguments[1].IsGenericParameter
                                   select method).First().MakeGenericMethod(new Type[] { typeof(SomeViewModel), typeof(string) });

Above code method is for "TResult Max[TSource,TResult](System.Collections.Generic.IEnumerable1[TSource], System.Func2[TSource,TResult])".
It was working fine in .net framework 4.7.2 , but while testing the migrated code in .net 6 it throws "ArrayOutOfBoundsException" So i tried changing the above code to following for getting same result from Enumerable methods in .Net 6

  var maxOfStringMethod = (from method in typeof(Enumerable).GetMethods()
                where method.Name == "Max"
                      && method.GetParameters().Length == 2 &&
                      method.GetParameters()[0].ParameterType.GenericTypeArguments[0].IsGenericParameter
                select method).ElementAt(1).MakeGenericMethod(new Type[] { typeof(SomeViewModel), typeof(string) });

Now it's throwing "System.ArgumentException: 'The type or method has 1 generic parameter(s), but 2 generic argument(s) were provided. A generic argument must be provided for each generic parameter.' " I dont know what to do to solve this next , any help will be much appreciated





Aucun commentaire:

Enregistrer un commentaire