Class ExpressionExtensions
- Namespace
- Htmx.Components.Extensions
- Assembly
- Htmx.Components.dll
Provides extension methods for working with LINQ expressions in the HTMX Components library.
public static class ExpressionExtensions
- Inheritance
-
ExpressionExtensions
- Inherited Members
Remarks
These extensions simplify common expression manipulation tasks such as extracting property names and types from lambda expressions used in table column definitions and input model building.
Methods
GetMemberType<T>(Expression<Func<T, object>>)
Extracts the type of the property from an expression like x => x.Property
public static Type GetMemberType<T>(this Expression<Func<T, object>> expression) where T : class
Parameters
expression
Expression<Func<T, object>>The lambda expression that accesses a property.
Returns
- Type
The CLR type of the property, with nullable types unwrapped to their underlying type.
Type Parameters
T
The type containing the property.
Examples
Expression<Func<User, object>> expr = u => u.Age; // where Age is int?
Type propertyType = expr.GetMemberType(); // Returns typeof(int)
Remarks
This method automatically unwraps nullable types to return the underlying type. For example, if the property is of type int?, this method returns typeof(int). This is useful for type-specific processing such as input control selection or formatting operations.
Exceptions
- ArgumentException
Thrown when the expression is not a member access expression.
- ArgumentNullException
Thrown when
expression
is null.
GetPropertyName<T>(Expression<Func<T, object>>)
Extracts the property name from an expression like x => x.Property
public static string GetPropertyName<T>(this Expression<Func<T, object>> expression)
Parameters
expression
Expression<Func<T, object>>The lambda expression that accesses a property.
Returns
- string
The name of the property referenced in the expression.
Type Parameters
T
The type containing the property.
Examples
Expression<Func<User, object>> expr = u => u.Name;
string propertyName = expr.GetPropertyName(); // Returns "Name"
Remarks
This method supports both direct member access (x => x.Property) and member access with type conversion (x => (object)x.Property).
Exceptions
- ArgumentException
Thrown when the expression is not a member access expression.
- ArgumentNullException
Thrown when
expression
is null.
GetPropertyName<T, TProp>(Expression<Func<T, TProp>>)
Extracts the property name from a strongly-typed expression like x => x.Property
public static string GetPropertyName<T, TProp>(this Expression<Func<T, TProp>> expression)
Parameters
expression
Expression<Func<T, TProp>>The lambda expression that accesses a property.
Returns
- string
The name of the property referenced in the expression.
Type Parameters
T
The type containing the property.
TProp
The type of the property being accessed.
Examples
Expression<Func<User, string>> expr = u => u.Name;
string propertyName = expr.GetPropertyName(); // Returns "Name"
Remarks
This overload provides type safety by avoiding boxing and allows for more specific property type handling.
Exceptions
- ArgumentException
Thrown when the expression is not a member access expression.
- ArgumentNullException
Thrown when
expression
is null.