Search code examples
c#compact-frameworkfunc.net-cf-3.5missingmethodexception

MissingMethodException for Func`2<> on WinCE although Func`2 depending code was already executed


I am working on a C# code running on .NETCF 3.5 on WindowsCE 6.0 that is throwing MissingMethodExceptions for Func`2<> during runtime. The code parts where the exception occurs is random.

The weird thing is, this happens when you already use the application for a while AND where definitely many calls to Func`2 already happened. (eg. via IEnumerable.Select() or .Where()) It seems that this behaviour starts if you just load enough Types during the lifetime of the application such that the sum of assembly file sizes exceeds ~18MB. But there is enough memory (RAM) available on the device to load the Type.

Also activated LoaderLogging but to no avail. It only shows me a TypeLoad error for Func`2.

As I ran out of ideas: What could be the cause of such errors?

Unfortunately I cannot share any code because it is 1) property of the company I work for and 2) many ten-thousand lines of code.


Solution

  • It seems there is a limit on .NETCF:

    One can only construct 1024 unique closed types per generic type declaration. (For more information see section "Limitations": http://blogs.msdn.com/b/romanbat/archive/2005/01/06/348114.aspx)

    Meaning:

    List<int> a;
    List<int> b;
    List<int> c;
    

    takes one "slot",

    List<int> d;
    List<string> e;
    

    takes two "slots" (two unique closings), and so on.

    Tricky thing was: Usually this raises an ArgumentException, but sometimes .NETCF throws MissingMethodExceptions instead. (see "3. Differences in exceptions thrown.": http://blogs.msdn.com/b/nazimms/archive/2005/01/25/360324.aspx)

    We reduced the usings of Func`2 by using our own Delegate Types where possible and this has solved the problem.