0

I am attempting to compile a c# script (class) on the fly and run it from an Arcmap AddIn (the script contains no ArcObjects). My unit test, in Visual Studio 2013, works just correctly and I am able to compile the c# code, invoke the constructor, and call my method with params and return the expected string value. When the same code is invoked from the ArcMap AddIn, I receive this exception from the method of the compiled class:

Could not load type 'Oldow.Runt.Core.Models.ExpressionObject' from assembly 'Oldow.Runt.Desktop.AddIn

So from the AddIn , the compilation is successful and so is the invoking the constructor. The exception is thrown from the compiled assembly's Run method.

This is my compilation code (no errors or warnings during compilation):

    Microsoft.CSharp.CSharpCodeProvider csProvider = new Microsoft.CSharp.CSharpCodeProvider();
    CompilerParameters options = new CompilerParameters();
    options.GenerateExecutable = false; 
    options.GenerateInMemory = true; 
    options.ReferencedAssemblies.Add(Assembly.GetExecutingAssembly().Location);
    CompilerResults result;
    result = csProvider.CompileAssemblyFromSource(options, code);

And here's an excerpt of the execution code for running the compiled script (TypeLoadException occurs in scriptObject.Run():

    var exportedTypes = compiledScript.GetExportedTypes();

    foreach (Type type in exportedTypes)
    {
        foreach (Type iface in type.GetInterfaces())
        {
            if (iface == typeof(ICustomExpressionEvaluator))
            {
                ConstructorInfo constructor = type.GetConstructor(System.Type.EmptyTypes);
                if (constructor != null && constructor.IsPublic)
                {   var scriptObject = constructor.Invoke(null) as ICustomExpressionEvaluator;
                    if (scriptObject != null)
                    {
                        returnText = scriptObject.Run(inputs);

Has anyone encountered this before in the AddIn framework and figured out how to resolve?


For reference, here's the test method that successfully runs.

[TestMethod]
public void CompileScriptTest()
{
    ExpressionObject p = new ExpressionObject()
    {
        SKTNumber = "asdasd3312"
    };

    string scriptText = @"namespace Oldow.Runt.Scripting {
using Oldow.Runt.Core.Models; 
public class XScript1 : ICustomExpressionEvaluator
{
public string Run(object[] expressionInputs)
{
    var plan = expressionInputs[0] as ExpressionObject;

    return string.Format(""Test SKT Number: {0}"",plan.SKTNumber);
}
}}";

    object[] inputs = new object[] { p };

    var text = ScriptCompiler.EvaluateExpression(
        scriptText,
        inputs);

    Assert.AreEqual("Test SKT Number: asdasd3312", text);   
}
1

I'm calling this a workaround since I'm not sure if there is a better way to deal with the typeloadexception, but I changed my CompilerParameters to generate to a file: outpath (where the outpath is the assembly cache directory + somefilename.dll). Now it works.

CompilerParameters options = new CompilerParameters();
options.GenerateExecutable = false; 
options.OutputAssembly = outpath;
options.GenerateInMemory = false;

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.