Class CSharpHelper
C# dynamic compilation related utilities.
Namespace: Neon.CSharp
Assembly: Neon.CSharp.dll
Syntax
public static class CSharpHelperProperties
RuntimeReferenceAssemblies
Returns the reference assembles for the current .NET runtime environment. These are required when dynamically compiling C# code.
Declaration
public static IEnumerable<PortableExecutableReference> RuntimeReferenceAssemblies { get; }Property Value
| Type | Description | 
|---|---|
| IEnumerable<PortableExecutableReference> | 
Exceptions
| Type | Condition | 
|---|---|
| NotSupportedException | Thrown when the current runtime environment is not recognized or supported. | 
Methods
Compile(string, string, Action<MetadataReferences>, CSharpCompilationOptions)
Compiles C# source code into an assembly.
Declaration
public static MemoryStream Compile(string source, string assemblyName, Action<MetadataReferences> referenceHandler = null, CSharpCompilationOptions compilerOptions = null)Parameters
| Type | Name | Description | 
|---|---|---|
| string | source | The C# source code. | 
| string | assemblyName | The generated assembly name. | 
| Action<MetadataReferences> | referenceHandler | Called to manage metadata/assembly references (see remarks). | 
| CSharpCompilationOptions | compilerOptions | Optional compilation options. This defaults to building a release assembly. | 
Returns
| Type | Description | 
|---|---|
| MemoryStream | The compiled assembly as a MemoryStream. | 
Remarks
By default, this method will compile the assembly with the standard reference assemblies for the currently executing runtime.
You may customize these by passing a referenceHandler
action.  This is passed the list of MetadataReference instances.
You can add or remove references as required.  The easiest way to add
a reference is to use type reference like:
using Microsoft.CodeAnalysis;
...
var source   = "public class Foo {}";
var assembly = CSharpHelper.Compile(source, "my-assembly",
references =>
{
references.Add(typeof(MyClass));    // Adds the assembly containing MyClass.
});
Exceptions
| Type | Condition | 
|---|---|
| CompilerErrorException | Thrown for compiler errors. |