Skip to content

Commit

Permalink
Xml comments.
Browse files Browse the repository at this point in the history
  • Loading branch information
mikasoukhov committed Feb 18, 2025
1 parent b809ccc commit 7b6278d
Show file tree
Hide file tree
Showing 9 changed files with 370 additions and 4 deletions.
24 changes: 24 additions & 0 deletions Linq/AsyncEnumerableExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,18 @@

using Ecng.Common;

/// <summary>
/// The extensions for <see cref="IAsyncEnumerable{T}"/>.
/// </summary>
public static class AsyncEnumerableExtensions
{
/// <summary>
/// Converts the <see cref="IAsyncEnumerable{T}"/> to the array.
/// </summary>
/// <typeparam name="T">The type of the elements of the <see cref="IAsyncEnumerable{T}"/>.</typeparam>
/// <param name="enu">The enumeration.</param>
/// <param name="cancellationToken">The cancellation token.</param>
/// <returns>The array of the elements of the <see cref="IAsyncEnumerable{T}"/>.</returns>
public static async ValueTask<T[]> ToArrayAsync2<T>(this IAsyncEnumerable<T> enu, CancellationToken cancellationToken)
{
if (enu is null)
Expand All @@ -22,6 +32,13 @@ public static async ValueTask<T[]> ToArrayAsync2<T>(this IAsyncEnumerable<T> enu
return [.. list];
}

/// <summary>
/// Gets the first element of the <see cref="IAsyncEnumerable{T}"/>.
/// </summary>
/// <typeparam name="T">The type of the elements of the <see cref="IAsyncEnumerable{T}"/>.</typeparam>
/// <param name="enu">The enumeration.</param>
/// <param name="cancellationToken">The cancellation token.</param>
/// <returns>The first element of the <see cref="IAsyncEnumerable{T}"/>.</returns>
public static async ValueTask<T> FirstAsync2<T>(this IAsyncEnumerable<T> enu, CancellationToken cancellationToken)
{
if (enu is null)
Expand All @@ -33,6 +50,13 @@ public static async ValueTask<T> FirstAsync2<T>(this IAsyncEnumerable<T> enu, Ca
throw new InvalidOperationException();
}

/// <summary>
/// Gets the first element of the <see cref="IAsyncEnumerable{T}"/> or the default value if the enumeration is empty.
/// </summary>
/// <typeparam name="T">The type of the elements of the <see cref="IAsyncEnumerable{T}"/>.</typeparam>
/// <param name="enu">The enumeration.</param>
/// <param name="cancellationToken">The cancellation token.</param>
/// <returns>The first element of the <see cref="IAsyncEnumerable{T}"/> or the default value if the enumeration is empty.</returns>
public static async ValueTask<T> FirstOrDefaultAsync2<T>(this IAsyncEnumerable<T> enu, CancellationToken cancellationToken)
{
if (enu is null)
Expand Down
51 changes: 51 additions & 0 deletions Linq/ExpressionExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -7,11 +7,25 @@

using Ecng.Common;

/// <summary>
/// The extensions for <see cref="Expression"/>.
/// </summary>
public static class ExpressionExtensions
{
/// <summary>
/// Gets the constant value from the expression.
/// </summary>
/// <typeparam name="T">The type of the constant value.</typeparam>
/// <param name="e">The expression.</param>
/// <returns>The constant value.</returns>
public static T GetConstant<T>(this Expression e)
=> ((ConstantExpression)e).Value.To<T>();

/// <summary>
/// Evaluates the expression and returns its value.
/// </summary>
/// <param name="e">The expression to evaluate.</param>
/// <returns>The evaluated value.</returns>
public static object Evaluate(this Expression e)
{
//A little optimization for constant expressions
Expand All @@ -21,6 +35,12 @@ public static object Evaluate(this Expression e)
return Expression.Lambda(e).Compile().DynamicInvoke();
}

/// <summary>
/// Replaces the source provider in the expression with the specified query provider.
/// </summary>
/// <param name="expression">The expression to modify.</param>
/// <param name="provider">The query provider to set.</param>
/// <exception cref="ArgumentNullException">Thrown when expression or provider is null.</exception>
public static void ReplaceSource(this Expression expression, IQueryProvider provider)
{
if (expression is null)
Expand All @@ -36,6 +56,11 @@ public static void ReplaceSource(this Expression expression, IQueryProvider prov
field.SetValue(expression, provider);
}

/// <summary>
/// Removes quote expressions from the given expression.
/// </summary>
/// <param name="e">The expression to process.</param>
/// <returns>The unquoted expression.</returns>
public static Expression StripQuotes(this Expression e)
{
while (e.NodeType == ExpressionType.Quote)
Expand All @@ -46,6 +71,13 @@ public static Expression StripQuotes(this Expression e)
return e;
}

/// <summary>
/// Gets the value of the specified member from the given instance.
/// </summary>
/// <param name="member">The member whose value to retrieve.</param>
/// <param name="instance">The object instance from which to retrieve the value.</param>
/// <returns>The value of the member.</returns>
/// <exception cref="NotSupportedException">Thrown when the member type is not supported.</exception>
public static object GetMemberValue(this MemberInfo member, object instance)
{
if (member is PropertyInfo pi)
Expand All @@ -56,6 +88,11 @@ public static object GetMemberValue(this MemberInfo member, object instance)
throw new NotSupportedException();
}

/// <summary>
/// Retrieves the innermost member in a nested member expression.
/// </summary>
/// <param name="exp">The member expression to process.</param>
/// <returns>The innermost member expression.</returns>
public static MemberExpression GetInnerMember(this MemberExpression exp)
{
if (exp.Expression is MemberExpression d)
Expand All @@ -64,6 +101,14 @@ public static MemberExpression GetInnerMember(this MemberExpression exp)
return exp;
}

/// <summary>
/// Evaluates the expression and returns its value as the specified type.
/// </summary>
/// <typeparam name="TValue">The type to convert the evaluated value to.</typeparam>
/// <param name="exp">The expression to evaluate.</param>
/// <returns>The evaluated value converted to the specified type.</returns>
/// <exception cref="ArgumentNullException">Thrown when the expression is null.</exception>
/// <exception cref="ArgumentOutOfRangeException">Thrown when the expression type is not supported.</exception>
public static TValue GetValue<TValue>(this Expression exp)
{
if (exp is null)
Expand All @@ -77,6 +122,12 @@ public static TValue GetValue<TValue>(this Expression exp)
throw new ArgumentOutOfRangeException(exp.NodeType.ToString());
}

/// <summary>
/// Converts the given expression type to its equivalent <see cref="ComparisonOperator"/>.
/// </summary>
/// <param name="type">The expression type.</param>
/// <returns>The corresponding <see cref="ComparisonOperator"/>.</returns>
/// <exception cref="ArgumentOutOfRangeException">Thrown when the expression type has no corresponding operator.</exception>
public static ComparisonOperator ToOperator(this ExpressionType type)
{
return type switch
Expand Down
Loading

0 comments on commit 7b6278d

Please sign in to comment.