LisaCore 0.0.38
See the version list below for details.
dotnet add package LisaCore --version 0.0.38
NuGet\Install-Package LisaCore -Version 0.0.38
<PackageReference Include="LisaCore" Version="0.0.38" />
paket add LisaCore --version 0.0.38
#r "nuget: LisaCore, 0.0.38"
// Install LisaCore as a Cake Addin #addin nuget:?package=LisaCore&version=0.0.38 // Install LisaCore as a Cake Tool #tool nuget:?package=LisaCore&version=0.0.38
LisaCore
1 Code Processor
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.
2 Requirements
.NET Core 3.1 or later
3 Getting Started
3.1 Install the library
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
3.2 Initialize CodeProcessor
Create an instance of the CodeProcessor class:
using LisaCore.Interpreter;
var codeProcessor = new CodeProcessor();
3.3 Add references and namespaces
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");
3.4 Execute code
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}");
}
4 Advanced Usage
4.1 Callbacks
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
4.2 Code validation
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());
}
}
4.3 Custom Class
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}");
}
4.4 Custom Function
Your Custome Function
// CustomFunctions.cs
namespace MyCustomFunctions
{
public static class MathFunctions
{
public static int Square(int number)
{
return number * number;
}
}
}
Add a reference to the assembly containing the custom function and the namespace in the CodeProcessor:
//Add reference to the assembly containing the custom functions
codeProcessor.AddReference(typeof(MathFunctions).Assembly);
//Add the namespace containing the custom functions
codeProcessor.AddNamespace("MyCustomFunctions");
Your code can reference and use the custom function:
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}");
}
4.5 Using DbContext for database operations
//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 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 was computed. 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. |
-
net7.0
- BrickSchema.Net (>= 1.3.20)
- Mages (>= 2.0.1)
- MathNet.Numerics (>= 5.0.0)
- Microsoft.CodeAnalysis.CSharp.Scripting (>= 4.6.0)
- Microsoft.CodeAnalysis.Scripting (>= 4.6.0)
- Microsoft.EntityFrameworkCore.InMemory (>= 7.0.5)
- Microsoft.EntityFrameworkCore.Sqlite (>= 7.0.5)
- Microsoft.EntityFrameworkCore.SqlServer (>= 7.0.5)
- Microsoft.ML (>= 2.0.1)
- Microsoft.ML.OnnxRuntime (>= 1.15.0)
- Microsoft.ML.OnnxTransformer (>= 2.0.1)
- Microsoft.ML.TimeSeries (>= 2.0.1)
- Microsoft.Recognizers.Text.DateTime (>= 1.8.8)
- UnidecodeSharpCore (>= 2.0.1)
- WeCantSpell.Hunspell (>= 4.0.0)
NuGet packages
This package is not used by any NuGet packages.
GitHub repositories
This package is not used by any popular GitHub repositories.
Version | Downloads | Last updated |
---|---|---|
0.0.50 | 301 | 7/14/2023 |
0.0.49 | 220 | 7/14/2023 |
0.0.48 | 217 | 7/14/2023 |
0.0.47 | 215 | 7/13/2023 |
0.0.46 | 198 | 6/19/2023 |
0.0.45 | 213 | 6/19/2023 |
0.0.44 | 234 | 6/19/2023 |
0.0.43 | 225 | 6/15/2023 |
0.0.42 | 230 | 6/15/2023 |
0.0.41 | 215 | 6/15/2023 |
0.0.40 | 238 | 6/8/2023 |
0.0.39 | 246 | 6/8/2023 |
0.0.38 | 232 | 6/8/2023 |
0.0.37 | 215 | 6/8/2023 |
0.0.36 | 224 | 6/8/2023 |
0.0.35 | 249 | 6/8/2023 |
0.0.34 | 236 | 6/8/2023 |
0.0.33 | 238 | 6/7/2023 |
0.0.32 | 212 | 6/7/2023 |
0.0.31 | 215 | 6/6/2023 |
0.0.30 | 237 | 6/2/2023 |
0.0.29 | 226 | 6/2/2023 |
0.0.28 | 197 | 6/1/2023 |
0.0.27 | 224 | 6/1/2023 |
0.0.26 | 173 | 6/1/2023 |
0.0.25 | 166 | 6/1/2023 |
0.0.24 | 199 | 6/1/2023 |
0.0.23 | 232 | 5/31/2023 |
0.0.22 | 207 | 5/31/2023 |
0.0.21 | 197 | 5/31/2023 |
0.0.20 | 193 | 5/23/2023 |
0.0.19 | 187 | 5/22/2023 |
0.0.18 | 202 | 5/22/2023 |
0.0.17 | 205 | 5/22/2023 |
0.0.16 | 192 | 5/22/2023 |
0.0.15 | 181 | 5/22/2023 |
0.0.14 | 208 | 5/18/2023 |
0.0.13 | 202 | 5/17/2023 |
0.0.12 | 191 | 5/12/2023 |
0.0.11 | 224 | 5/11/2023 |
0.0.10 | 218 | 5/11/2023 |
0.0.9 | 190 | 5/10/2023 |
0.0.7 | 193 | 4/25/2023 |
0.0.6 | 186 | 4/25/2023 |
0.0.5 | 181 | 4/24/2023 |
0.0.3 | 193 | 4/21/2023 |
0.0.2 | 215 | 4/6/2023 |
0.0.1 | 202 | 4/4/2023 |
Beta