I have old delphi 7 application with lib "FastScript". This application can compile runtime code from text and call imported methods from main application.
i need create this functional for my web application writen on C# .NET10 Blazor.
I can compile assembly from text in memory and call method "Main" from compilled assembly. Method Main writing to my web application console.
Assembly code:
using System;
public class MyAssemblyClass {
public static void Main(string[] args) {
Console.WriteLine("hello world");
}
}
Calling assembly method "Main" from web application
SyntaxTree parsedSyntaxTree = Parse(textsource, "", CSharpParseOptions.Default.WithLanguageVersion(LanguageVersion.CSharp12));
CSharpCompilation? compilation = CSharpCompilation.Create(Frame.AssemblyClassName, [parsedSyntaxTree], DefaultReferences, exe_DefaultCompilationOptions);
EmitResult result = default!;
Exception? exception = default!;
using (var ms = new MemoryStream())
{
try
{
result = compilation.Emit(ms);
if (result.Success)
{
Type? FrameType = CompilleType(ms, Frame);
if (FrameType != null)
{
object obj = Activator.CreateInstance(FrameType);
FrameType.InvokeMember("Main",
BindingFlags.Default | BindingFlags.InvokeMethod,
null,
obj,
new object[]{});
}
else throw new Exception("Invoke error.");
}
}
catch (Exception ex)
{
exception = ex;
}
}
I need export some method from my web application to my assembly and call this method.