SecureCSharpEval 25.2.26
There is a newer version of this package available.
See the version list below for details.
See the version list below for details.
dotnet add package SecureCSharpEval --version 25.2.26
NuGet\Install-Package SecureCSharpEval -Version 25.2.26
This command is intended to be used within the Package Manager Console in Visual Studio, as it uses the NuGet module's version of Install-Package.
<PackageReference Include="SecureCSharpEval" Version="25.2.26" />
For projects that support PackageReference, copy this XML node into the project file to reference the package.
<PackageVersion Include="SecureCSharpEval" Version="25.2.26" />
<PackageReference Include="SecureCSharpEval" />
For projects that support Central Package Management (CPM), copy this XML node into the solution Directory.Packages.props file to version the package.
paket add SecureCSharpEval --version 25.2.26
The NuGet Team does not provide support for this client. Please contact its maintainers for support.
#r "nuget: SecureCSharpEval, 25.2.26"
#r directive can be used in F# Interactive and Polyglot Notebooks. Copy this into the interactive tool or source code of the script to reference the package.
#addin nuget:?package=SecureCSharpEval&version=25.2.26
#tool nuget:?package=SecureCSharpEval&version=25.2.26
The NuGet Team does not provide support for this client. Please contact its maintainers for support.
SecureCSharpEval
SecureCSharpEval is a secure and sandboxed C# script execution library that allows users to evaluate C# code dynamically while ensuring safety and preventing malicious activities.
Features
- Execute C# scripts dynamically with controlled parameters.
- Prevents unsafe operations such as file access, process execution, and system manipulation.
- Configurable execution timeout and memory limits.
- Optimized for .NET – Fully compatible with .NET 6/7/8/9.
- Optional shell execution support (DISABLED by default for security).
Installation
You can install this library via NuGet Package Manager:
Install-Package SecureCSharpEval
Usage
- Basic Usage
var eval = new SecureCSharpEval(new CodeValidator(), new ProcessExecutor());
string code = "return x + y;";
var parameters = new Dictionary<string, object>
{
{"x", 10},
{"y", 20}
};
object? result = await eval.EvaluateAsync(code, parameters);
Console.WriteLine($"Result: {result}");
- Using Conditional Statements and Loops
string code = @"
int sum = 0;
for (int i = 0; i < n; i++)
{
if (i % 2 == 0)
{
sum += i;
}
}
return sum;
";
var parameters = new Dictionary<string, object>
{
{"n", 10}
};
object? result = await eval.EvaluateAsync(code, parameters);
Console.WriteLine($"Result: {result}");
Configuring Execution Limits
eval.ExecutionTimeoutMs = 3000; // Increase timeout to 3 seconds
eval.MaxMemoryUsageMb = 100; // Set max memory usage to 100 MB
eval.AllowShellExecution = true; // Enable shell execution (use with caution)
Security Considerations
- By default, execution is sandboxed and restricted from running system commands.
- If enabling AllowShellExecution, ensure proper input validation.
- Avoid allowing user-provided scripts without proper review.
Contact
For any questions, feel free to contact me or create an issue in the repository.
Product | Versions Compatible and additional computed target framework versions. |
---|---|
.NET | net6.0 is compatible. net6.0-android was computed. net6.0-ios was computed. net6.0-maccatalyst was computed. net6.0-macos was computed. net6.0-tvos was computed. net6.0-windows was computed. net7.0 is compatible. net7.0-android was computed. net7.0-ios was computed. net7.0-maccatalyst was computed. net7.0-macos was computed. net7.0-tvos was computed. net7.0-windows was computed. net8.0 is compatible. net8.0-android was computed. net8.0-browser was computed. net8.0-ios was computed. net8.0-maccatalyst was computed. net8.0-macos was computed. net8.0-tvos was computed. net8.0-windows was computed. net9.0 is compatible. net9.0-android was computed. net9.0-browser was computed. net9.0-ios was computed. net9.0-maccatalyst was computed. net9.0-macos was computed. net9.0-tvos was computed. net9.0-windows was computed. |
Compatible target framework(s)
Included target framework(s) (in package)
Learn more about Target Frameworks and .NET Standard.
-
net6.0
- Microsoft.CodeAnalysis.CSharp (>= 4.13.0)
- Microsoft.CodeAnalysis.CSharp.Scripting (>= 4.13.0)
-
net7.0
- Microsoft.CodeAnalysis.CSharp (>= 4.13.0)
- Microsoft.CodeAnalysis.CSharp.Scripting (>= 4.13.0)
-
net8.0
- Microsoft.CodeAnalysis.CSharp (>= 4.13.0)
- Microsoft.CodeAnalysis.CSharp.Scripting (>= 4.13.0)
-
net9.0
- Microsoft.CodeAnalysis.CSharp (>= 4.13.0)
- Microsoft.CodeAnalysis.CSharp.Scripting (>= 4.13.0)
NuGet packages
This package is not used by any NuGet packages.
GitHub repositories
This package is not used by any popular GitHub repositories.