D
D
Davilkus Games2022-01-22 11:57:14
C++ / C#
Davilkus Games, 2022-01-22 11:57:14

Can scripts be imported or compiled at runtime?

Can this be done somehow? It is hardly possible to compile, because usually other scripts are recompiled, but is it possible to import somehow?

Answer the question

In order to leave comments, you need to log in

1 answer(s)
U
Ukrainskiy, 2022-01-22
@Davilkus

Can

using System;
using System.Collections.Generic;
using System.Linq;
using Microsoft.CSharp;
using System.CodeDom.Compiler;

class Program
{
    static void Main(string[] args)
    {
        var csc = new CSharpCodeProvider(new Dictionary<string, string>() { { "CompilerVersion", "v3.5" } });
        var parameters = new CompilerParameters(new[] { "mscorlib.dll", "System.Core.dll" }, "foo.exe", true);
        parameters.GenerateExecutable = true;
        CompilerResults results = csc.CompileAssemblyFromSource(parameters,
        @"using System.Linq;
            class Program {
              public static void Main(string[] args) {
                var q = from i in Enumerable.Range(1,100)
                          where i % 2 == 0
                          select i;
              }
            }");
        results.Errors.Cast<CompilerError>().ToList().ForEach(error => Console.WriteLine(error.ErrorText));
        Assembly assembly = results.CompiledAssembly;
        Type program = assembly.GetType("Program");
        MethodInfo main = program.GetMethod("Main");
        main.Invoke(null, null);
    }
}

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question