Search Results for

    Show / Hide Table of Contents

    Class CodeBuilder

    Helper functions for creating and loading dynamic code

    Inheritance
    object
    CodeBuilder
    Implements
    ICodeBuilder
    Inherited Members
    object.GetType()
    object.MemberwiseClone()
    object.ToString()
    object.Equals(object)
    object.Equals(object, object)
    object.ReferenceEquals(object, object)
    object.GetHashCode()
    Namespace: LemonEdge.API.Core.Scripting
    Assembly: LemonEdge.API.dll
    Syntax
    public class CodeBuilder : ICodeBuilder
    Remarks

    See https://msdn.microsoft.com/en-us/magazine/mt808499.aspx

    For example to load code dynamically:

    private (Assembly, byte[]) LoadCode(string dllName, string code)
    {
        var syntax = CodeBuilder.ParseSyntaxTree(code);
        using (var m = new MemoryStream())
        {
            var compiled = CodeBuilder.Compile(dllName, m, syntax);
            if (compiled.Success)
            {
                var bytes = m.ToArray();
                var assem = CodeBuilder.LoadGeneratedAssembly(bytes);
                return (assem, bytes);
            }
            else throw new Exception("Unable to generate code");
        }
    }
    LemonEdge.CodeGenerator

    Constructors

    CodeBuilder(ILogger<CodeBuilder>)

    Declaration
    public CodeBuilder(ILogger<CodeBuilder> log = null)
    Parameters
    Type Name Description
    ILogger<CodeBuilder> log

    Properties

    GetStandardExecutableReferences

    Returns all PortableExecutableReference assemblies that have been loaded by the system for use when generating dynamic code

    Declaration
    public IEnumerable<PortableExecutableReference> GetStandardExecutableReferences { get; }
    Property Value
    Type Description
    IEnumerable<PortableExecutableReference>

    Methods

    AddOrUpdateMemoryType(Assembly, byte[])

    Adds a single dynamic memory type assembly to always be included.

    Declaration
    public void AddOrUpdateMemoryType(Assembly assem, byte[] data)
    Parameters
    Type Name Description
    Assembly assem

    The assembly to ensure we are including a PortableExecutableReference

    byte[] data

    Compile(string, string, bool, params Assembly[])

    Compiles the given syntax tree into an assembly in the specified dllStream with the specified fileName

    Declaration
    public CustomAssembly Compile(string fileName, string code, bool throwErrors, params Assembly[] references)
    Parameters
    Type Name Description
    string fileName

    The name to give the generated assembly.

    string code

    The code to parse and return a syntax tree for.

    bool throwErrors

    Whether to throw errors or return null.

    Assembly[] references

    The reference dlls to compile against.

    Returns
    Type Description
    CustomAssembly

    THe result of the compilation of the dll, including any compile errors, warnings, etc

    EnsureHasType(Assembly)

    Ensures the list of executable references include the specified assem assembly

    Declaration
    public Task EnsureHasType(Assembly assem)
    Parameters
    Type Name Description
    Assembly assem

    The assembly to ensure we are including a PortableExecutableReference

    Returns
    Type Description
    Task

    A task for loading the specified assembly reference if SetAssemblyReferenceGenerator(Func<Assembly, Task<PortableExecutableReference>>) has been used.

    GetPortableExecutableReferences(params byte[][])

    Gets the PortableExecutableReference array from a byte array.

    Declaration
    public PortableExecutableReference[] GetPortableExecutableReferences(params byte[][] references)
    Parameters
    Type Name Description
    byte[][] references

    The byte array.

    Returns
    Type Description
    PortableExecutableReference[]

    The PortableExecutableReference array.

    LoadStandardExecutableReferences(IEnumerable<Assembly>, IEnumerable<byte[]>)

    When generating dynamic dlls, and compiling code, it will often make reference to libraries within LemonEdge itself such as this Utils assembly, and the API.

    As such this is used to load PortableExecutableReference classes for each assembly in the app domain that can be referenced by dynamic code being generated

    You shouldn't need to use this, LemonEdge does itself to ensure all relevant AddIn and assemblies can be referenced in custom code

    Declaration
    public Task<PortableExecutableReference[]> LoadStandardExecutableReferences(IEnumerable<Assembly> assemblies, IEnumerable<byte[]> customAssemblies = null)
    Parameters
    Type Name Description
    IEnumerable<Assembly> assemblies

    The list of assemblies we want to generate PortableExecutableReference for

    IEnumerable<byte[]> customAssemblies

    Any list of custom assemblies in their byte array form that we also want to be referenceable

    Returns
    Type Description
    Task<PortableExecutableReference[]>

    A task for completing the method - only used if the SetAssemblyReferenceGenerator(Func<TResult>) has been used to provide a custom mechanism of creating the classes

    ParseSyntaxTree(string, CSharpParseOptions)

    Returns a syntax tree for the given custom c# code

    Declaration
    public SyntaxTree ParseSyntaxTree(string code, CSharpParseOptions options = null)
    Parameters
    Type Name Description
    string code

    The code to parse and return a syntax tree for.

    CSharpParseOptions options

    Options support script format (top-level code with no class declarations).

    Returns
    Type Description
    SyntaxTree

    A syntax tree for the given custom c# code

    SetAssemblyReferenceGenerator(Func<Assembly, Task<PortableExecutableReference>>)

    Normally we can generate a PortableExecutableReference from a file assembly easily.

    However in certain enviroments, such as Blazor, we dont have the assemblies location so we need to download it from an http stream.

    This method allows us to override the default generation of a PortableExecutableReference with a custom one

    Declaration
    public void SetAssemblyReferenceGenerator(Func<Assembly, Task<PortableExecutableReference>> getRef)
    Parameters
    Type Name Description
    Func<Assembly, Task<PortableExecutableReference>> getRef

    A function that given an assembly will return a task that can generate a PortableExecutableReference for it

    Validate(string, string, params Assembly[])

    Compiles the given syntax tree in validation mode, returning any errors.

    Declaration
    public ImmutableArray<Diagnostic> Validate(string name, string code, params Assembly[] references)
    Parameters
    Type Name Description
    string name

    The temporary name for the generated assembly.

    string code

    The code to parse and return a syntax tree for.

    Assembly[] references

    The reference dlls to compile against.

    Returns
    Type Description
    ImmutableArray<Diagnostic>

    The result of the validation, including any compile errors, warnings, etc

    Implements

    ICodeBuilder

    Extension Methods

    LinqExtensions.AsArray<T>(T)
    LinqExtensions.ToArrayOfOne<T>(T)
    LinqExtensions.ToListOfOne<T>(T)
    MiscExtensions.SetIfNotEqual<T, TP>(T, Expression<Func<T, TP>>, TP)
    WeakReferenceExtensions.WeakReference(object)
    SQLExtensions.ToSQLValue(object, bool)
    ReflectionExtensions.ClearEventInvocations(object, string)
    StringExtensions.ToCSVFormatString(object, Type)
    In this article
    Back to top © LemonEdge Technologies. All rights reserved.