Hi, I was compiling some code dynamically with Core3.1 and was told that the platform didn't support it, but using the Framework was fine.I don't know how to return a responsibility, ask everybody to help
using RRQMSocket;
using RRQMSocket.RPC;
using System;
using System.CodeDom.Compiler;
using System.Collections.Generic;
using System.IO;
using System.Reflection;
namespace RPCServiceCoreDemo
{
class Program
{
static void Main(string[] args)
{
var source = @"
using System;
namespace Sample
{
public class Program
{
public static void Main()
{
Console.WriteLine(""hello, world"");
}
}
}";
var a = GetAssemblyFromSourceByCodeDom(source);
var methodA = a.CreateInstance("Sample.Program").GetType().GetMethod("Main");
methodA.Invoke(null, null);
Console.ReadKey();
}
static Assembly GetAssemblyFromSourceByCodeDom(params string[] source)
{
using var provider = CodeDomProvider.CreateProvider(
"CSharp",
new Dictionary<string, string> {
{ "CompilerVersion", "v4.0" }});
var compileResult = provider.CompileAssemblyFromSource(
new CompilerParameters()
{
IncludeDebugInformation = false,
TreatWarningsAsErrors = true,
WarningLevel = 4,
GenerateExecutable = false,
GenerateInMemory = true
},
source);
if (compileResult.Errors.Count > 0)
{
throw new ArgumentException();
}
return compileResult.CompiledAssembly;
}
}
}