![]() |
VOOZH | about |
dotnet add package LisaCore --version 0.0.50
NuGet\Install-Package LisaCore -Version 0.0.50
<PackageReference Include="LisaCore" Version="0.0.50" />
<PackageVersion Include="LisaCore" Version="0.0.50" />Directory.Packages.props
<PackageReference Include="LisaCore" />Project file
paket add LisaCore --version 0.0.50
#r "nuget: LisaCore, 0.0.50"
#:package LisaCore@0.0.50
#addin nuget:?package=LisaCore&version=0.0.50Install as a Cake Addin
#tool nuget:?package=LisaCore&version=0.0.50Install as a Cake Tool
LisaCore Code Processor .NET library provides a powerful and flexible way to execute C# code at runtime. With the ability to add references, import namespaces, and execute code with optional callbacks, this library is a valuable tool for developers looking to add dynamic code execution capabilities to their applications.
.NET Core 3.1 or later
To use the CodeProcessor library, first, add a reference to the library in your project. Install package from NuGet. https://www.nuget.org/packages/LisaCore
Create an instance of the CodeProcessor class:
using LisaCore.Interpreter;
var codeProcessor = new CodeProcessor();
Add the necessary assembly references and namespaces for your code. By default, some common namespaces are already imported. Default assemblies:
LisaCore Code Processor loads all relevant assemblies from your project and
assemblies for default namespaces below.
Default namespaces:
System
System.IO
System.Linq
System.Text
System.Collections.Generic
Microsoft.EntityFrameworkCore
Loading your custom namespaces:
codeProcessor.AddReference(Assembly.Load("MyAssembly"));
codeProcessor.AddNamespace("MyNamespace");
Execute C# code snippets by calling the ExecuteAsync method:
string code = "return 1 = 2;";
var (result, error) = await codeProcessor.ExcecuteAsync(code);
if(error != null)
{
Console.WriteLine($"Error: {error.Message}");
}
else
{
Console.WriteLine($"Result: {result}");
}
You can also provide a callback function to return a value during code processing or get the result of the executed code:
async Task MyCallBack(object? result)
{
Console.WriteLine($"Result from callback: {result}");
}
string code = "int a = 1; Callback(a); return a + 3;";
await codeProcessor.ExecuteAsync(code, MyCallBack);
Output:
Result from callback: 1
Result from callback: 4
To check whether a code snippet is valid without executing it, call the IsCodeValid method:
string code = "return 5 - 3;";
bool isValid = codeProcessor.IsCodeValid(code, out var diagnostics);
if (isValid)
{
Console.Writeline("Code is valid.");
}
else
{
Console.WriteLine("Code is not valid. Errors:");
foreach (var diagnostic in diagnostics)
{
Console.WriteLine(diagnostic.ToString());
}
}
As noted in section 3.3, LisaCore Code Processor loaded all relevant assemblies form your project. However, you need to reference namespaces you want to use in our script. There are two methods to load assembly.
Use AddNamespace function. This method is persistance and allows you to reference MyApp.Models in all of your scripts.
codeProcessor.AddNamespace("MyApp.Models");
Include using statement in your script. The using namespace only apply to current code.
string code =@" using MyApp.Models; var person = new Person { Name = ""John Doe"", Age = 30 }; return person.Name;";
Completed code:
codeProcessor.AddNamespace("MyApp.Models");
string code = @"
var person = new Person { Name = ""John Doe"", Age = 30 };
return person.Name;";
var (result, error) = await codeProcessor.ExecuteAsync(code);
if (error != null)
{
Console.WriteLine($"Error: {error.Message}");
}
else
{
Console.WriteLine($"Result: {result}");
}
// CustomFunctions.cs
namespace MyCustomFunctions
{
public static class MathFunctions
{
public static int Square(int number)
{
return number * number;
}
}
}
//Add reference to the assembly containing the custom functions
codeProcessor.AddReference(typeof(MathFunctions).Assembly);
//Add the namespace containing the custom functions
codeProcessor.AddNamespace("MyCustomFunctions");
string code = "return MathFunctions.Square(5);";
var (result, error) = await codeProcessor.ExecuteAsync(code);
if (error != null)
{
Console.WriteLine($"Error: {error.Message}");
}
else
{
Console.WriteLine($"Result: {result}");
}
//your EF code
var dbContext = new MyDbContext();
string code = @"
var customers = DbContext.Set<Customer>().ToList();
return customers.Count;";
var (result, error) = await codeProcessor.ExecuteAsync(code, null, dbContext);
if (error != null)
{
Console.WriteLine($"Error: {error.Message}");
}
else
{
Console.WriteLine($"Result: {result}");
}
| Product | Versions Compatible and additional computed target framework versions. |
|---|---|
| .NET | net7.0 net7.0 is compatible. net7.0-android net7.0-android was computed. net7.0-ios net7.0-ios was computed. net7.0-maccatalyst net7.0-maccatalyst was computed. net7.0-macos net7.0-macos was computed. net7.0-tvos net7.0-tvos was computed. net7.0-windows net7.0-windows was computed. net8.0 net8.0 was computed. net8.0-android net8.0-android was computed. net8.0-browser net8.0-browser was computed. net8.0-ios net8.0-ios was computed. net8.0-maccatalyst net8.0-maccatalyst was computed. net8.0-macos net8.0-macos was computed. net8.0-tvos net8.0-tvos was computed. net8.0-windows net8.0-windows was computed. net9.0 net9.0 was computed. net9.0-android net9.0-android was computed. net9.0-browser net9.0-browser was computed. net9.0-ios net9.0-ios was computed. net9.0-maccatalyst net9.0-maccatalyst was computed. net9.0-macos net9.0-macos was computed. net9.0-tvos net9.0-tvos was computed. net9.0-windows net9.0-windows was computed. net10.0 net10.0 was computed. net10.0-android net10.0-android was computed. net10.0-browser net10.0-browser was computed. net10.0-ios net10.0-ios was computed. net10.0-maccatalyst net10.0-maccatalyst was computed. net10.0-macos net10.0-macos was computed. net10.0-tvos net10.0-tvos was computed. net10.0-windows net10.0-windows was computed. |
This package is not used by any NuGet packages.
This package is not used by any popular GitHub repositories.
| Version | Downloads | Last Updated |
|---|---|---|
| 0.0.50 | 622 | 7/14/2023 |
| 0.0.49 | 510 | 7/14/2023 |
| 0.0.48 | 500 | 7/14/2023 |
| 0.0.47 | 491 | 7/13/2023 |
| 0.0.46 | 480 | 6/19/2023 |
| 0.0.45 | 508 | 6/19/2023 |
| 0.0.44 | 500 | 6/19/2023 |
| 0.0.43 | 509 | 6/15/2023 |
| 0.0.42 | 488 | 6/15/2023 |
| 0.0.41 | 485 | 6/15/2023 |
| 0.0.40 | 513 | 6/8/2023 |
| 0.0.39 | 513 | 6/8/2023 |
| 0.0.38 | 531 | 6/8/2023 |
| 0.0.37 | 477 | 6/8/2023 |
| 0.0.36 | 506 | 6/8/2023 |
| 0.0.35 | 545 | 6/8/2023 |
| 0.0.34 | 512 | 6/8/2023 |
| 0.0.33 | 533 | 6/7/2023 |
| 0.0.32 | 487 | 6/7/2023 |
| 0.0.31 | 495 | 6/6/2023 |
Beta