DynamicCSharp 1.0.0

There is a newer version of this package available.
See the version list below for details.
dotnet add package DynamicCSharp --version 1.0.0
NuGet\Install-Package DynamicCSharp -Version 1.0.0
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="DynamicCSharp" Version="1.0.0" />
For projects that support PackageReference, copy this XML node into the project file to reference the package.
paket add DynamicCSharp --version 1.0.0
#r "nuget: DynamicCSharp, 1.0.0"
#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.
// Install DynamicCSharp as a Cake Addin
#addin nuget:?package=DynamicCSharp&version=1.0.0

// Install DynamicCSharp as a Cake Tool
#tool nuget:?package=DynamicCSharp&version=1.0.0

DynamicCSharp

Overview

DynamicCSharp is a small library designed for use on .NET Core 2.1 or later. With the Windows implementation of the .NET framework, we could use CodeDom classes to parse C# source code and compile it to a dynamically-loaded assembly. With .NET core, these classes are replaced by the Roslyn compiler API, a complex suite of classes that support syntax tree generation and semantic analysis in addition to emitting output assemblies.

DynamicCSharp provides a simple interface and Facade onto these Roslyn classes, so that users who just want to compile C# source to a dynamically loaded assembly at run-time have a simple library that does this.

Source code

The source code for this library is maintained here on GitHub. The code is published under the MIT License, a copy of which is included at the bottom of this documentation page.

Using the library

Detailed documentation for this package is also to be found on GitHub. To give you a flavour of how the library might be used, see the example below.

Example

The following is an example from one of the unit tests for this package:

public class TestClass
{
    private string source = @"
            using System;
            namespace Fred
            {
                public class Joe
                {
                    private int i = 0;
                    public int GetNextInt()
                    {
                        return ++i;
                    }
                }
            }";

    [Fact]
    public void CanInvokeEmittedMethods()
    {
        ICompiler c = Compiler.Create();
        c.AssemblyName = "Assem10";
        c.AddReferences(new string[] { "System.Int32", "System.Double", "System.IO.Path" });
        c.Source = source;
        c.Compile();
        Type type = c.Assembly.ExportedTypes.Where(t => t.Name == "Joe").FirstOrDefault();
        var joe = Activator.CreateInstance(type);
        Assert.IsType(type, joe);
        var getNextInt = (Func<int>)Delegate
            .CreateDelegate(typeof(Func<int>), joe, type.GetMethod("GetNextInt"));
        Assert.Equal(1, getNextInt());
        Assert.Equal(2, getNextInt());
    }
}

Licensing

This product is published under the standard MIT License as described at https://opensource.org/licenses/MIT. The specific wording for this license is as follows:

Copyright 2018 Ropley Information Technology Ltd.

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 net5.0 was computed.  net5.0-windows was computed.  net6.0 was computed.  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 was computed.  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. 
.NET Core netcoreapp2.1 is compatible.  netcoreapp2.2 was computed.  netcoreapp3.0 was computed.  netcoreapp3.1 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 785 1/30/2019
1.0.0 793 9/4/2018