C
C#•3mo ago
IcyIme

I want to make a dynamic compiler of c# code and with that have a console on the web page, using thi

I want to make a dynamic compiler of c# code and with that have a console on the web page, using this it will be possible to interact with the code
8 Replies
IcyIme
IcyIme•3mo ago
but i do not know how and where to startt
using Microsoft.AspNetCore.Mvc;
using Microsoft.CodeAnalysis.CSharp.Scripting;
using Microsoft.CodeAnalysis.Scripting;
using System;
using System.Threading.Tasks;

namespace DynamicScriptingAPI.Controllers
{
[ApiController]
[Route("api/[controller]")]
public class ScriptController : ControllerBase
{
[HttpPost]
public async Task<IActionResult> ExecuteScript([FromBody] ScriptRequest request)
{
try
{
// Compile the code
Script<object> script = CSharpScript.Create(request.Code)
.WithOptions(ScriptOptions.Default
.WithReferences(typeof(Console).Assembly));

// Execute the code
var result = await script.RunAsync();

// Return the result
return Ok(result.ReturnValue);
}
catch (CompilationErrorException ex)
{
return BadRequest(ex.Message);
}
catch (Exception ex)
{
return StatusCode(500, ex.Message);
}
}
}

public class ScriptRequest
{
public string Code { get; set; }
}
}
using Microsoft.AspNetCore.Mvc;
using Microsoft.CodeAnalysis.CSharp.Scripting;
using Microsoft.CodeAnalysis.Scripting;
using System;
using System.Threading.Tasks;

namespace DynamicScriptingAPI.Controllers
{
[ApiController]
[Route("api/[controller]")]
public class ScriptController : ControllerBase
{
[HttpPost]
public async Task<IActionResult> ExecuteScript([FromBody] ScriptRequest request)
{
try
{
// Compile the code
Script<object> script = CSharpScript.Create(request.Code)
.WithOptions(ScriptOptions.Default
.WithReferences(typeof(Console).Assembly));

// Execute the code
var result = await script.RunAsync();

// Return the result
return Ok(result.ReturnValue);
}
catch (CompilationErrorException ex)
{
return BadRequest(ex.Message);
}
catch (Exception ex)
{
return StatusCode(500, ex.Message);
}
}
}

public class ScriptRequest
{
public string Code { get; set; }
}
}
Pobiega
Pobiega•3mo ago
You might want to look at the source code for modix and the code execution bit. I don't have the links right now thou
Pobiega
Pobiega•3mo ago
GitHub
GitHub - discord-csharp/MODiX: Discord Bot handling basic moderatio...
Discord Bot handling basic moderation needs, soon implements statistics. - discord-csharp/MODiX
Pobiega
Pobiega•3mo ago
GitHub
GitHub - discord-csharp/CSharpRepl: CSharpRepl is a web api for C# ...
CSharpRepl is a web api for C# REPL using the Roslyn Scripting APIs - discord-csharp/CSharpRepl
Pobiega
Pobiega•3mo ago
Found it
IcyIme
IcyIme•3mo ago
i do not know how to use it 😦
Pobiega
Pobiega•3mo ago
You are asking for something incredibly dangerous and non-trivial to implement. So.. do some research These are not "just use this and all works fine" solutions
Unknown User
Unknown User•3mo ago
Message Not Public
Sign In & Join Server To View