CSharpScript - TypeLoadException

121 views Asked by At

I am using the Microsoft.CodeAnalysis.CSharp.Scripting's CSharpScript (version 3.4.0-beta2-final) to execute custom calculations. Some calculations are not defined directly in the script, but delegated to a compiled static methods of class MyPlugin.Calculations which resides in MyPlugin assembly (only calling the static method).

When I try to invoke a static method of the Calculations class from the script

return Calculations.GetHpPipingPrice();

It throws this exception:

TypeLoadException: Could not load type 'MyPlugin.Calculations' from assembly 'MyPlugin.Data, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null'.

I don't understand, why it tries to load the type Calculations from MyPlugin.Data assembly when it is defined in MyPlugin assembly. I added reference to the MyPlugin assembly and imported the MyPlugin namespace into the ScriptOptions. What is the problem here? Could this be, that I have a plugin system and load the MyPlugin assembly using Assembly.LoadFrom method? The MyPlugin is not in the same directory as the executing assembly. The MyPlugin.Data assembly contains data, which are used for initialization (it also contains body of the scripts).

Edit: This is how the script is created:

var compiledScript = CSharpScript.Create(Body, globalsType: InputType,
                options: ScriptOptions.Default.AddReferences(AssemblyReferences).AddImports(Imports));

CompiledScript = compiledScript.CreateDelegate();

Where the AssemblyReferences contains the assembly MyPlugin (and also others) and Imports contains the MyPlugin namespace (and also others).

0

There are 0 answers