OperatorBuilder
Create operator overloads (binary and unary operators).
See also: Emit Overview | TypeBuilder | ConversionOperatorBuilder
Factory Methods — Comparison
| Method |
Description |
Equality(type) |
Create an equality operator (==) |
Inequality(type) |
Create an inequality operator (!=) |
LessThan(type) |
Create a less-than operator (<) |
GreaterThan(type) |
Create a greater-than operator (>) |
LessThanOrEqual(type) |
Create a less-than-or-equal operator (<=) |
GreaterThanOrEqual(type) |
Create a greater-than-or-equal operator (>=) |
Factory Methods — Arithmetic
| Method |
Description |
Addition(type) |
Create an addition operator (+) |
Subtraction(type) |
Create a subtraction operator (-) |
Multiplication(type) |
Create a multiplication operator (*) |
Division(type) |
Create a division operator (/) |
Modulus(type) |
Create a modulus operator (%) |
Factory Methods — Unary
| Method |
Description |
UnaryPlus(type) |
Create a unary plus operator (+) |
UnaryMinus(type) |
Create a unary minus operator (-) |
LogicalNot(type, returnType?, parameterName?) |
Create a logical negation operator (!) |
BitwiseComplement(type) |
Create a bitwise complement operator (~) |
Increment(type) |
Create an increment operator (++) |
Decrement(type) |
Create a decrement operator (--) |
True(type) |
Create a true operator |
False(type) |
Create a false operator |
Factory Methods — Bitwise
| Method |
Description |
BitwiseAnd(type) |
Create a bitwise AND operator (&) |
BitwiseOr(type) |
Create a bitwise OR operator (\|) |
ExclusiveOr(type) |
Create a bitwise XOR operator (^) |
LeftShift(type) |
Create a left shift operator (<<) |
RightShift(type) |
Create a right shift operator (>>) |
UnsignedRightShift(type) |
Create an unsigned right shift operator (>>>) |
Factory Methods — Generic
| Method |
Description |
Binary(op, leftType, rightType, returnType) |
Create any binary operator |
Unary(op, operandType, returnType) |
Create any unary operator |
All comparison and arithmetic factory methods accept optional leftParameterName and rightParameterName parameters (default: "left" and "right"). Unary factory methods accept an optional parameterName (default: "operand").
Body
// Expression body
op.WithExpressionBody("left.Equals(right)")
// Block body
op.WithBody(body => body
.AddStatement("var result = left.Value == right.Value;")
.AddReturn("result"))
Conditional Compilation
op.When(Directives.Net7OrGreater)
op.InRegion("Operators")
Attributes & XML Documentation
op.WithAttribute("MethodImpl", a => a.WithArgument("MethodImplOptions.AggressiveInlining"))
op.WithXmlDoc("Compares two values for equality.")
op.WithXmlDoc(doc => doc.Summary("Compares two values for equality."))
op.WithInheritDoc()
op.AddUsing("System.Runtime.CompilerServices")
Example
// Standalone builder
var equality = OperatorBuilder.Equality("CustomerId")
.WithExpressionBody("left.Equals(right)");
typeBuilder.AddOperator(equality)
// Convenience methods on TypeBuilder (recommended)
TypeBuilder.Struct("CustomerId")
.AddEqualityOperator("left.Equals(right)")
.AddInequalityOperator("!left.Equals(right)")
// Lambda configuration
TypeBuilder.Struct("Money")
.AddOperator(_ => OperatorBuilder.Addition("Money")
.WithExpressionBody("new Money(left.Amount + right.Amount)"))
.AddOperator(_ => OperatorBuilder.UnaryMinus("Money")
.WithExpressionBody("new Money(-operand.Amount)"))
// Custom binary operator with different types
TypeBuilder.Struct("Vector")
.AddOperator(_ => OperatorBuilder.Binary("*", "Vector", "int", "Vector")
.WithExpressionBody("new Vector(left.X * right, left.Y * right)"))
// Bitwise operators
TypeBuilder.Struct("Flags")
.AddOperator(_ => OperatorBuilder.BitwiseAnd("Flags")
.WithExpressionBody("new Flags(left.Value & right.Value)"))
.AddOperator(_ => OperatorBuilder.BitwiseOr("Flags")
.WithExpressionBody("new Flags(left.Value | right.Value)"))
Properties
op.Operator // string — the operator symbol
op.IsUnary // bool — true for unary, false for binary
op.ReturnType // string
op.OperandType // string — left operand type (or sole operand for unary)
op.RightType // string? — right operand type (binary only)