Search code examples
c#.netextension-methodsmissingmethodexception

Calling an extension method statically throws MissingMethodException


I have an extension method that splits a set of items into smaller subsets:

static class MyClass {
    /// <summary>
    /// Breaks a list of items into chunks of a specific size
    /// </summary>
    public static IEnumerable<IEnumerable<T>> chunk<T>(this IEnumerable<T> source, int chunksize)
    {
        while (source.Any())
        {
            yield return source.Take(chunksize);
            source = source.Skip(chunksize);
        }
    }
}

Now I may call this method either via

  • MyClass.chunk(myEnumerable, 500), or by using
  • myEnumerable.chunk(200)

Right? But one of my customer complains about a MissingMethodException when I use the static (the first) call:

System.MissingMethodException: Method not found: System.Collections.Generic.List´1<System.Collections.Generic.List´1<!!0>> MyClass.chunk(System.Collections.Generic.IEnumerable´1<!!0>, Int32).).

I'm not able no reproduce it, maybe with your help I may...


Solution

  • Did you notice that your method returns a IEnumerable<IEnumerable<T>, but the error message is talking about a method returning a List<List<T>>?

    If I would have to guess, I'd say that you've changed the return type of your extension method at some point. You then replaced only the DLL containing your extension method, and now some other, dependent DLL is trying to invoke your extension method as it used to be... but it's of course no longer there.

    You'll have to recompile & re-deploy all assemblies that reference / invoke your extension method.

    P.S.: Whether you invoke your extension method via MyClass.chunk(myEnumerable, 500) or via myEnumerable.chunk(200) does not make any difference at all; the latter style of calling an extension method is a syntactic shortcut for the former, the C# compiler will automatically transform extension method calls into the static method call it really is.