Wierd TypeLoadException in windows store app. (Unity 4.3.1, Windows 8.1)

Code below generates very strange TypeLoadException. If you comment foreach call in BogusClass everything will work fine.

Note that BogusClass.BogusFunct() method neither called or referenced in any place. If you just declare this class with access to HashSet or Dictionary, you’ll break program. Also not all method calls of HashSet leads to error.

it’s all very mysterious.

using UnityEngine;
using System.Collections;
using System.Collections.Generic;



public class Test : MonoBehaviour {

	void Start () {
        Debug.Log("Started");


        try
        {
            TestClass[] _arr = new TestClass[] 
            {
                TestClass.A,
                TestClass.B,
            };
        }
        catch (System.Exception e)
        {
            //TypeLoadException
            throw e;
        }


	
	}
	
	void Update () {
	
	}
}

public class BogusClass
{
    private readonly System.Collections.Generic.HashSet<int> mContainer = new System.Collections.Generic.HashSet<int>();

    protected Dictionary<int, string> dict = new Dictionary<int, string>();

    public void BogusFunct()
    {
        //comment RemoveWhere call, and everything will work fine
        mContainer.RemoveWhere(e => true);

        //also bug, try to comment this GetEnumerator call 
        foreach (var v in dict)
        {

        }

    }
}

public sealed class ThisClassGeneratesTypeLoadException<E>
{
    private ThisClassGeneratesTypeLoadException()
    {
    }

    public static ThisClassGeneratesTypeLoadException<T> of<T>(params T[] elements)
    {
        return null;
    }

}

public class TestClass
{

    public static readonly TestClass A = new TestClass();
    public static readonly TestClass B = new TestClass();


    public static readonly ThisClassGeneratesTypeLoadException<TestClass> tst = ThisClassGeneratesTypeLoadException<TestClass>.of(A, B);

}

This issue is part of more huge problem, that stops development of Metro plugin. Could someone recommend workaround?

When I try, this line causes me the issue:

No exception when it is commented out.

I also tried to cleate an instance of BogusClass and call BogusFunct(), no exception as long as that line is commented out.

Do you save issue when you move the code from script to plugin dll?

Yes, metro plugin has same behavior. Actually I started from plugin.

I can’t understand why unity/mono make some kind of instrumentation of /metro/*.dll. If I reference visual studio 2013 produced dll from generated solution, everything also fine.

What is your build target? x86? Master/Debug?

x86 Debug.

I’m going more deep.
Instrumented assembly that unity/mono created based on /metro/*.dll has some differences in static constructors for this line

public static readonly ThisClassGeneratesTypeLoadException<TestClass> tst = ThisClassGeneratesTypeLoadException<TestClass>.of(A, B);

“normal” dll has this call

IL_002c:  call       class Plugin.ThisClassGeneratesTypeLoadException`1<!!0> class Plugin.ThisClassGeneratesTypeLoadException`1<class Plugin.TestClass>::of<class Plugin.TestClass>(!!0[])

dll created by unity/mono has this:

IL_002c:  call       class Plugin.ThisClassGeneratesTypeLoadException`1<!!0> Plugin.ThisClassGeneratesTypeLoadException`1::of<class Plugin.TestClass>(!!0[])

and it’s totally breaks runtime.

Looks like some glitches in template arguments.

So, fix is to remove function template argument;

public sealed class ThisClassGeneratesTypeLoadException<T>
{
	private ThisClassGeneratesTypeLoadException()
	{}

	public static ThisClassGeneratesTypeLoadException<T> of(params T[] elements)
	{
		return null;
	}
}

P.S. Still have no clue, why metro assembly instrumented this way.

Please, submit this as a bug report.
Thanks.