DynamicCSharp 1.0.0

Class library that wraps the C# Roslyn compiler, so that a C# string can be compiled and loaded as a dynamic assembly at runtime.

Install-Package DynamicCSharp -Version 1.0.0
dotnet add package DynamicCSharp --version 1.0.0
paket add DynamicCSharp --version 1.0.0
The NuGet Team does not provide support for this client. Please contact its maintainers for support.

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.

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.

Version History

Version Downloads Last updated
1.0.0 46 9/4/2018