← Back to API Reference

RuleParameter

Defines a parameter for rule expression compilation and execution.

public class RuleParameter

Constructor

public RuleParameter(string name, Type type, object? value = null)
Parameter Description
name Parameter name used in expressions
type CLR type for compilation
value Optional runtime value (can be null for compile-only)

Factory Methods

ForCompile(string name, Type type)

Creates a parameter for compilation only — no value needed.

var compileParam = RuleParameter.ForCompile("customer", typeof(Customer));
workflow.Compile(new[] { compileParam });

ForExecute(string name, Type type, object value)

Creates a parameter for execution — requires a real value.

var customer = new Customer { Name = "Alice", Age = 25 };
var executeParam = RuleParameter.ForExecute("customer", typeof(Customer), customer);
var results = workflow.Execute(new[] { executeParam });

Properties

Property Type Description
Name string Parameter name
Type Type CLR type
Value object? Runtime value
HasValue bool True if a runtime value is set

Compile vs Execute

Phase What Matters Factory Method
Compile Name + Type only (value can be null) ForCompile(name, type)
Execute Name + Type + Value (all required) ForExecute(name, type, value)
// Compile at startup — types only
var compileParams = new[]
{
    RuleParameter.ForCompile("customer", typeof(Customer))
};
workflow.Compile(compileParams);

// Execute later with real data
var executeParams = new[]
{
    RuleParameter.ForExecute("customer", typeof(Customer), customer)
};
var results = workflow.Execute(executeParams);

Why separate them?

  • Compile once at startup (no data needed)
  • Execute many times with different data
  • Prevents accidental name/type mismatches between compile and execute

One Parameter per Rule

A rule takes exactly one RuleParameter. To use several values, wrap them in a struct/class and reference its members in the expression.

public sealed class Person
{
    public string Name { get; set; } = "";
    public int Age { get; set; }
}

var parameter = new RuleParameter("p", typeof(Person), new Person { Name = "Alice", Age = 25 });

var rule = new Rule
{
    Description = "Adult named Alice",
    Expression = "p.Name.Length > 0 && p.Age >= 18"
};

var workflow = new Workflow { Rules = { rule } };
workflow.Compile(new RuleParameter("p", typeof(Person)));
var result = workflow.Execute(parameter).First();

The parameter name in the expression must match exactly:

// Expression references the single parameter by name
var rule = new Rule { Expression = "point.X > point.Y" };
var workflow = new Workflow { Rules = { rule } };
workflow.Compile(new RuleParameter("point", typeof(Point)));

Limitations:

  • Exactly one parameter per rule — wrap multiple values in a struct/class
  • The parameter name must be a valid C# identifier
  • The parameter type must be resolvable in the compiler’s context


Back to top

MIT License. Built with Roslyn + Typed Delegates.