Skip to content
Auto
Products

ExpressionParserOptions

Namespace: TagBites.Expressions
Assembly: TagBites.Expressions.dll
Product: TagBites.Expressions v. 1.5.0

Options for parsing expressions, including validation, parameters, global members, and custom property resolution.

public class ExpressionParserOptions

Inheritance: object → ExpressionParserOptions

An instance becomes read-only the first time it is used for parsing. Use Fork(Type, Type, IList<(Type, string)>, bool?) to reuse the prepared, shared settings while varying the result type or parameters.

Initializes a new options instance with the default settings.

public ExpressionParserOptions()

ExpressionParserOptions(ExpressionParserOptions)

Section titled “ExpressionParserOptions(ExpressionParserOptions)”

Initializes a new mutable options instance by copying every setting from other.

public ExpressionParserOptions(ExpressionParserOptions other)

Indicates whether to allow reflection. Default: false.

public bool AllowReflection { get; set; }

bool

True to allow runtime casting using custom operators. Default: false.

public bool AllowRuntimeCast { get; set; }

bool

Custom operators:

typeis(someExpression, "MyNamespace.MyType,MyAssembly")
typeas(someExpression, "MyNamespace.MyType,MyAssembly")
typecast(someExpression, "MyNamespace.MyType,MyAssembly")

True to allow < / <= / > / >= on strings, compared ordinally via String.Compare(string, string). Not valid in real C#. Default: false.

public bool AllowStringRelationalOperators { get; set; }

bool

True to allow throw expressions (for example x > 0 ? x : throw new ArgumentException()). Default: false.

public bool AllowThrowExpressions { get; set; }

bool

Function to resolve property/field-style access for types whose shape only exists at runtime, e.g. a database row, a CMS content type, a value that lives in another process.

public Func<IExpressionMemberResolverContext, Expression> CustomPropertyResolver { get; set; }

Func<IExpressionMemberResolverContext, Expression>

List of global members (values or delegates). Member type is optional; if null, the type is based on the value. If both member type and value are null, the type is object. Member with name ‘this’ can be access implicitly (when UseFirstParameterAsThis is false).

public IDictionary<string, (Type, object)> GlobalMembers { get; set; }

IDictionary<string, (Type, object)>

True to disable the fixed set of common framework types that are otherwise available by their short name regardless of IncludedTypes. Default: false.

public bool IgnoreBuiltInTypes { get; set; }

bool

The built-in types are for example: TimeSpan, DateTime, DateTimeOffset, DateTimeKind, DayOfWeek, StringComparison, CultureInfo, MidpointRounding, Math, Enumerable, List, Dictionary, HashSet, and Convert and more typically used in an expressions.
The C# primitive keywords (int, string, bool, etc.) are always available and are not affected by this option.

True to resolve parameters, variables, global members, type members and IncludedTypes case-insensitively. Default: false.

public bool IgnoreCase { get; set; }

bool

Collection of types that can be used in expressions.

public ICollection<Type> IncludedTypes { get; }

ICollection<Type>

List of parameters of the function.

public IList<(Type, string)> Parameters { get; set; }

IList<(Type, string)>

A type to convert the expression to, for example, to create a general lambda like Func<object>. If null, the result type is based on the expression.

public Type ResultCastType { get; set; }

Type

Expected and required result type of the expression. Used only for validation. If null, the result type is not checked.

public Type ResultType { get; set; }

Type

Can be overridden per fork through Fork(Type, Type, IList<(Type, string)>, bool?), because it is not part of the shared cached context.

Collection of types imported statically, as if using static was applied. Their public static methods, fields, properties and constants can be used unqualified. For example, adding Math makes Sqrt(x), Max(a, b), PI and E available. Members of instance parameters, global members and instance types always take precedence.

public ICollection<Type> StaticImports { get; }

ICollection<Type>

Function that resolves a type from its name, invoked when a type cannot be found among ResultType, Parameters, IncludedTypes or the built-in types. The name may be namespace-qualified (e.g. System.Text.StringBuilder) when that form is used in expression. A generic type name is suffixed with an apostrophe and the number of type arguments (e.g. List'1, Dictionary'2), and the returned type must be the corresponding open generic definition (e.g. typeof(List<>)). Return null to indicate the name is not recognized.

public Func<string, Type> TypeResolver { get; set; }

Func<string, Type>

True if the first parameter should be used as ‘this’ so its members can be accessed implicitly. Alternatively, the ‘this’ member in GlobalMembers can be used. Default: false.

public bool UseFirstParameterAsThis { get; set; }

bool

Caches reflected members (methods, indexers, extension methods) on this options instance. Default: false.

public bool UseMemberCache { get; set; }

bool

Fork(Type, Type, IList<(Type, string)>, bool?)

Section titled “Fork(Type, Type, IList<(Type, string)>, bool?)”

Creates a read-only variant that reuses the prepared, shared settings of this instance (global members, included types, static imports, member cache and the resolution flags), while overriding the result type, parameters or ‘this’ handling.

public ExpressionParserOptions Fork(Type resultType, Type resultCastType, IList<(Type, string)> parameters, bool? useFirstParameterAsThis)

ExpressionParserOptions

A prepared, read-only options instance that shares the reusable lookups of this instance. When every argument is null, this instance is returned, because the fork would be identical.

The shared member cache and settings are reused by every fork. Only the parameters and ‘this’ handling rebuild the parameter-specific part of the context. Forking makes this instance read-only, because its shared settings are now shared with the fork.