oleg-shilo / cs-script.core

.NET Core port of CS-Script
MIT License
100 stars 18 forks source link

Load compiled assemblies on scripts #26

Open duddo opened 3 years ago

duddo commented 3 years ago

Hi! I'm trying to load into the evaluator an Assembly produced by CompileAssemblyFromFile, but I can't use the referenced type inside the script. Maybe I'm using a wrong syntaxis? I put together a simpe example to explain what I'm trying to achieve.

Summer.cs:

using System;

class Summer { public int Sum(int a, int b) { return a + b; } }

Script.cs:

using System;

class Script { public int Execute() { Summer s = new Summer(); return s.Sum(10, 5); } }

Program.cs:

using CSScriptLib; using System;

namespace CsScript { class Program { static void Main(string[] _) { CSScript.Evaluator.Reset(false); //clear all referenced assemblies

        //compile dll
        string referencedAssemblyName = CSScript.Evaluator.CompileAssemblyFromFile(@".\Summer.cs", @".\Summer.dll");

        //load dll
        CSScript.Evaluator.ReferenceAssembly(referencedAssemblyName);

        //execute
        dynamic script = CSScript.Evaluator.LoadFile(@".\Script.cs");
        int result = script.Execute();

        Console.WriteLine(result);
    }
}

}

Thank you!

oleg-shilo commented 3 years ago

You can. You only need to make your class Summer public. . . . As a side note please use https://github.com/oleg-shilo/cs-script repository. This one is no longer active.

duddo commented 3 years ago

Sorry for the wrong repo, should I reopen the question in the other one?

Unfortunately, even making the class public, I still get a compile error: it can't found the symbol.

I'm trying to use this approach as a workaround Roslyn, to try and have my script in separate files. Do you have any advise against this approach? Thank you.