Search code examples
c#compiler-constructionunity-game-engine

NotSupportedException: The invoked member is not supported in a dynamic module in C# Unity 5


This is for my graduation project. The game idea briefly is to help students practice programming by providing some interesting missions that require a C# code solution something like codingame.com, when the user produces the correct output the result is visualised in a Unity scene.

I am using a C# compiler plugin to compile the user's code in the Unity 5 run-time. Everything is working fine (each scene alone start playing the scene in the editor and stopping it) but when I advance from scene to the next one this error rises when I build the user's C# code in the run-time (NotSupportedException: The invoked member is not supported in a dynamic module) (the error always rises in the second scene or the next scene).

The Error:

NotSupportedException: The invoked member is not supported in a dynamic module

Error

The Line that produces the error:

this.assemblyReferences = domain.GetAssemblies().Select(a => a.Location).ToArray();

assemblyRefrences is an array of strings: string[] assemblyReferences;

this line is in a script called ScriptBundleLoader in its constructor


Solution

  • The error happens because you are trying to get the location of an in-memory assembly (the ones you have created on the fly) and those assemblies aren't located anywhere.

    If you want to avoid this error try something like this:

    this.assemblyReferences = domain.GetAssemblies().Select(a => 
    { 
        try{ return a.Location; }catch{ return null; }
    
    }).Where(s => s != null).ToArray();
    

    EDIT:

    As Jon Skeet pointed it can be used "IsDynamic" instead of trapping the exception:

    this.assemblyReferences = domain.GetAssemblies()
        .Where(a => !a.IsDynamic)
        .Select(a => a.Location)
        .ToArray();