oleg-shilo / cs-script

C# scripting platform
http://www.cs-script.net
MIT License
1.56k stars 234 forks source link

CompileAssemblyFromFile with CompileInfo #335

Closed AzizDZH closed 1 year ago

AzizDZH commented 1 year ago

Hi Oleg Could you please add option to CompileAssemblyFromFile with given CompileInfo ? thanks

oleg-shilo commented 1 year ago

Not a prob. In a meantime you can use this extension method for that:

static class CSScriptExtensions
{
    public static string CompileAssemblyFromFile(this IEvaluator eval, string scriptFile, CompileInfo info)
    {
        MethodInfo compile = eval.GetType().GetMethod("Compile", BindingFlags.Instance | BindingFlags.NonPublic);
        compile.Invoke(eval, new object[] { null, scriptFile, info });
        return info.AssemblyFile;
    }
}

. . .

var script = Path.GetFullPath("test.cs");
File.WriteAllText(script,
                            @"using System;
                            public class Calc
                            {
                                public int Sum(int a, int b) => a+b;
                            }");

var info = new CompileInfo();
info.AssemblyFile = Path.GetFullPath(script + ".dll");
info.PdbFile = Path.ChangeExtension(info.AssemblyFile, ".pdb");

string asmFile = CSScript.CodeDomEvaluator.CompileAssemblyFromFile(script, info);
oleg-shilo commented 1 year ago

Done, will be available in the very next release.

oleg-shilo commented 1 year ago

Available now from nuget.org

AzizDZH commented 1 year ago

thanks a lot