← Back to API Reference

CompiledDelegate

Fast invocation wrappers for compiled delegates. Eliminates the overhead of DynamicInvoke by extracting the parameter value and calling the delegate directly. A rule takes exactly one parameter, so only single-parameter signatures (sync and async) are supported.

internal abstract class CompiledDelegate
internal sealed class CompiledFunc<TParam, TReturn> : CompiledDelegate
internal sealed class CompiledAction<TParam> : CompiledDelegate
internal sealed class CompiledAsyncFunc<TParam, TReturn> : CompiledDelegate
internal sealed class CompiledAsyncAction<TParam> : CompiledDelegate
internal static class CompiledDelegateFactory

Namespace: RoslynRules.Models

Key characteristics:

  • Abstract base (CompiledDelegate) with a single Invoke(object?) method
  • Typed wrappers for single-parameter delegates avoid DynamicInvoke overhead
  • Async wrappers support both sync-blocking (Invoke) and true async (InvokeAsync) paths
  • CompiledDelegateFactory uses reflection to auto-detect the correct wrapper type, and throws NotSupportedException for a delegate that does not have exactly one parameter
  • Marked with [RequiresUnreferencedCode] — may not work with trimming or AOT

CompiledDelegate (Base)

Methods

Method Return Description
Invoke(object? parameter) object? Invokes the delegate with the extracted parameter value

Typed Single-Parameter Wrappers

CompiledFunc<TParam, TReturn>

Wraps Func<TParam, TReturn>.

var del = new CompiledFunc<string, bool>(s => s.Length > 0);
var result = del.Invoke("hello"); // returns true (as object?)

CompiledAction<TParam>

Wraps Action<TParam> (void return).

var del = new CompiledAction<string>(s => Console.WriteLine(s));
var result = del.Invoke("hello"); // returns null

CompiledAsyncFunc<TParam, TReturn>

Wraps Func<TParam, Task<TReturn>>.

var del = new CompiledAsyncFunc<string, int>(async s =>
{
    await Task.Delay(1);
    return s.Length;
});

// Sync-blocking (uses ConfigureAwait(false) to avoid deadlock)
var result = del.Invoke("hello");

// True async
var resultAsync = await del.InvokeAsync("hello");

CompiledAsyncAction<TParam>

Wraps Func<TParam, Task> (async void).

var del = new CompiledAsyncAction<string>(async s =>
{
    await File.WriteAllTextAsync("log.txt", s);
});

del.Invoke("log entry");           // blocks
await del.InvokeAsync("log entry");  // async

CompiledDelegateFactory

Methods

Wrap(Delegate)

Creates the appropriate CompiledDelegate wrapper from a raw Delegate.

public static CompiledDelegate Wrap(Delegate del)

Detection logic:

  1. If the delegate does not have exactly 1 parameter → throws NotSupportedException
  2. If the return type is Task → CompiledAsyncAction<TParam>
  3. If the return type is Task<T> → CompiledAsyncFunc<TParam, T>
  4. If the return type is void → CompiledAction<TParam>
  5. Otherwise → CompiledFunc<TParam, TReturn>

Parameters

Parameter Type Description
del Delegate The raw compiled delegate to wrap

Returns

CompiledDelegate — The typed wrapper for fast invocation.

Attributes

[RequiresUnreferencedCode("RoslynRules uses reflection to inspect delegate signatures and instantiate generic wrappers. This code may not work correctly with trimming or AOT.")]


Usage Example

Basic wrapping with the factory

using RoslynRules.Models;

// Simple sync function
Func<string, bool> isValid = s => !string.IsNullOrEmpty(s);
var compiled = CompiledDelegateFactory.Wrap(isValid);

// Fast invoke (no DynamicInvoke)
var result = compiled.Invoke("hello"); // true

// Async function
Func<int, Task<int>> doubleAsync = async x => { await Task.Delay(1); return x * 2; };
var compiledAsync = CompiledDelegateFactory.Wrap(doubleAsync);

// Sync-blocking path
var syncResult = compiledAsync.Invoke(5); // 10

// True async path
var asyncResult = await ((CompiledAsyncFunc<int, int>)compiledAsync).InvokeAsync(5); // 10

Delegates with more than one parameter are rejected

Func<string, int, bool> checkLength = (s, max) => s.Length <= max;

// A rule takes exactly one parameter — Wrap throws for anything else
CompiledDelegateFactory.Wrap(checkLength); // ❌ NotSupportedException

// Wrap the extra values in a single type instead
public record LengthCheck(string Text, int Max);
Func<LengthCheck, bool> ok = c => c.Text.Length <= c.Max;
var compiled = CompiledDelegateFactory.Wrap(ok);
var result = compiled.Invoke(new LengthCheck("hello", 10)); // true

Integration with Rule execution

Note: CompiledDelegate, its wrappers, and CompiledDelegateFactory are internal. They are not part of the public API; the snippet below is conceptual and illustrates how the engine wraps a delegate internally. Application code should compile and execute rules through a Workflow (Workflow.Compile(...) / Workflow.Execute(...)) instead of constructing these types directly.

ExpressionCompiler.Compile<TDelegate> is generic and takes the parameter names as a string[] (not a RuleParameter[]). For a single-parameter Customer expression:

// Conceptual — illustrates internal wrapping.
var rawDelegate = compiler.Compile<Func<Customer, bool>>(
    "customer.IsActive",
    new[] { "customer" });

var compiled = CompiledDelegateFactory.Wrap(rawDelegate); // internal
var result = compiled.Invoke(customer); // true / false (as object?)

Performance Notes

Delegate Type Wrapper Invoke Overhead
Func<T, R> CompiledFunc<T, R> Direct cast + call (fast)
Action<T> CompiledAction<T> Direct cast + call (fast)
Func<T, Task<R>> CompiledAsyncFunc<T, R> Direct cast + block/await
Func<T, Task> CompiledAsyncAction<T> Direct cast + block/await

Every rule delegate takes exactly one parameter and is invoked through a typed wrapper — no DynamicInvoke. To pass several values, wrap them in a struct or class.



Back to top

MIT License. Built with Roslyn + Typed Delegates.