dynamic-code 1.0.1

dotnet add package dynamic-code --version 1.0.1
                    
NuGet\Install-Package dynamic-code -Version 1.0.1
                    
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="dynamic-code" Version="1.0.1" />
                    
For projects that support PackageReference, copy this XML node into the project file to reference the package.
<PackageVersion Include="dynamic-code" Version="1.0.1" />
                    
Directory.Packages.props
<PackageReference Include="dynamic-code" />
                    
Project file
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 dynamic-code --version 1.0.1
                    
#r "nuget: dynamic-code, 1.0.1"
                    
#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.
#:package dynamic-code@1.0.1
                    
#:package directive can be used in C# file-based apps starting in .NET 10 preview 4. Copy this into a .cs file before any lines of code to reference the package.
#addin nuget:?package=dynamic-code&version=1.0.1
                    
Install as a Cake Addin
#tool nuget:?package=dynamic-code&version=1.0.1
                    
Install as a Cake Tool

DynamicCodeFromString

NuGet .NET Tests Reasoning

A .NET 8 project for compiling and executing C# code from strings at runtime using Roslyn. This project demonstrates how to dynamically compile code, extract class and method information, and create delegates for runtime execution.

Features

  • Compile C# code from a string at runtime
  • Extract class and method names from code using Roslyn syntax trees
  • Match methods to delegate signatures
  • Create delegates for dynamically compiled methods
  • Example usage for both integer and string functions
  • Comprehensive test suite
  • Published as a NuGet package: dynamic-code

Requirements

Installation

Install the package from NuGet:

dotnet add package dynamic-code

Usage Example

using DynamicCode;

var code = @"
    using System;
    public static class DynamicClass
    {
        public static int Calculate(int x, int y)
        {
            return x * y;
        }
    }
";

var intFuncType = DelegateTypeBuilder.Create()
    .AddInput(typeof(int))
    .AddInput(typeof(int))
    .AddOutput(typeof(int))
    .BuildFuncType();

var intFn = DynamicCompiler.CompileFunction(intFuncType, code);

var intResult = (int)intFn.DynamicInvoke(7, 6);

Console.WriteLine($"Function body:\r\n'{code}'");
Console.WriteLine($"Function result is: {intResult}");

// The same thing, but in a different way

var intFn1 = DynamicCompiler.CompileFunction<Func<int, int, int>>(code);

intResult = intFn1(8, 9);

Console.WriteLine($"Function body:\r\n'{code}'");
Console.WriteLine("x = 8, y = 9");
Console.WriteLine($"Function result is: {intResult}");

Project Structure

  • DynamicCode/Compiler/ - Core compiler and utility classes
    • DynamicCompiler: Main API for compiling code and creating delegates
    • CompilerUtils: Utilities for extracting class and method names from syntax trees
    • DelegateTypeBuilder: Fluent builder for delegate types
    • SimpleDynamicCompiler: Minimal example for compiling a specific function signature
  • DynamicCode.Test/ - Unit tests for all major features
  • Program.cs: Example usage

Running Tests

To run the test suite:

dotnet test

License

MIT License

Copyright (c) 2025 Serhiy Krasovskyy xhunter74@gmail.com

Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.

Product Compatible and additional computed target framework versions.
.NET 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 was computed.  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.  net10.0 was computed.  net10.0-android was computed.  net10.0-browser was computed.  net10.0-ios was computed.  net10.0-maccatalyst was computed.  net10.0-macos was computed.  net10.0-tvos was computed.  net10.0-windows was computed. 
Compatible target framework(s)
Included target framework(s) (in package)
Learn more about Target Frameworks and .NET Standard.

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
1.0.1 164 5/29/2025
1.0.0 148 5/28/2025

2025-05-29 Refactored and extended GetClrTypeName.\n           Added XML documentations.\n           Extended tests.\n2025-05-28 Initial version