jeudi 11 avril 2019

Why Expression.Call is throwing parameters error

I'm trying to extract Count method, so I could reuse it later to build expression tree.

var g = Expression.Parameter(typeof(IEnumerable<float?>), "g");
var countMethod = typeof(Enumerable)
    .GetMethods()
    .Single(m => m.Name == "Count" && m.GetParameters().Count() == 1);
var countMaterialized = countMethod
    .MakeGenericMethod(new[] { g.Type });
var expr = Expression.Call(countMaterialized, g);

It throws this error:

System.ArgumentException: 'Expression of type 'System.Collections.Generic.IEnumerable1[System.Nullable1[System.Single]]' cannot be used for parameter of type 'System.Collections.Generic.IEnumerable1[System.Collections.Generic.IEnumerable1[System.Nullable1[System.Single]]]' of method 'Int32 Count[IEnumerable1](System.Collections.Generic.IEnumerable1[System.Collections.Generic.IEnumerable1[System.Nullable1[System.Single]]])''

What am I missing?





Aucun commentaire:

Enregistrer un commentaire