NetFabric.Reflection 4.1.0

Prefix Reserved
There is a newer version of this package available.
See the version list below for details.
dotnet add package NetFabric.Reflection --version 4.1.0                
NuGet\Install-Package NetFabric.Reflection -Version 4.1.0                
This command is intended to be used within the Package Manager Console in Visual Studio, as it uses the NuGet module's version of Install-Package.
<PackageReference Include="NetFabric.Reflection" Version="4.1.0" />                
For projects that support PackageReference, copy this XML node into the project file to reference the package.
paket add NetFabric.Reflection --version 4.1.0                
#r "nuget: NetFabric.Reflection, 4.1.0"                
#r directive can be used in F# Interactive and Polyglot Notebooks. Copy this into the interactive tool or source code of the script to reference the package.
// Install NetFabric.Reflection as a Cake Addin
#addin nuget:?package=NetFabric.Reflection&version=4.1.0

// Install NetFabric.Reflection as a Cake Tool
#tool nuget:?package=NetFabric.Reflection&version=4.1.0                

NetFabric.Reflection

To find if a type is enumerable, it's not enough to check if it implements IEnumerable, IEnumerable<>, or IAsyncEnumerable<>. foreach and await foreach support several other cases. This module contains extension methods that take into account all these cases.

Usage

IsEnumerable()

using NetFabric.Reflection;

var isEnumerable = type.IsEnumerable(out var enumerableInfo);

var isAsyncEnumerable = type.IsAsyncEnumerable(out var asyncEnumerableInfo);

The methods return a boolean value indicating if it's a valid enumerable or enumerator.

If true, the output parameter contains MethodInfo for the method GetEnumerator/GetAsynEnumerator of the enumerable, the property Current and the method MoveNext/MoveNextAsync of the enumerator, following the precedences used by Roslyn for the foreach and await foreach keywords. It may also contain for methods Reset and Dispose/DisposeAsync if defined.

ExpressionEx

NetFabric.Reflection contains high level Expression generators that makes it easier to handle enumerables in Expression Trees. The code generated is as similar as possible to the one generated by Roslyn for the equivalent keywords.

To use these, add the NetFabric.Reflection package to your project.

ExpressionEx.ForEach

public static Expression ForEach(Expression enumerable, Func<Expression, Expression> body)
  • enumerable - Defines an enumerable.
  • body - Defines the body containing the code performed for each item. Pass a lambda expression that, given an Expression that defines an item, returns an Expression that uses it.

WARNING: Async enumerables are not supported.

The Expression generated depends on:

  • Whether the enumerator is an interface, class, struct, or ref struct.
  • Whether the enumerator is disposable or not.
  • Whether the enumerable is an array. In this case, it uses the array indexer instead of IEnumerable<> to enumerate.

Throws an exception if the Expression in the first parameter does not define an enumerable. In case you don't want the exception to be thrown, use the other overload that takes an EnumerableInfo or EnumerableSymbols for the first parameter. Use IsEnumerable to get the required values.

Here's an example, using ExpressionEx.ForEach, that calculates the sum of the items in an enumerable:

using static NetFabric.Expressions.ExpressionEx;
using static System.Linq.Expressions.Expression;

int Sum<TEnumerable>(TEnumerable enumerable)
{
    var enumerableParameter = Parameter(typeof(TEnumerable), "enumerable");
    var sumVariable = Variable(typeof(int), "sum");
    var expression = Block(
        new[] {sumVariable},
        Assign(sumVariable, Constant(0)),
        ForEach(
            enumerableParameter,
            item => AddAssign(sumVariable, item)),
        sumVariable);
    var sum = Lambda<Func<TEnumerable, int>>(expression, enumerableParameter).Compile();

    return sum(enumerable);
}

ExpressionEx.For

public static Expression For(Expression initialization, Expression condition, Expression iterator, Expression body) 
  • initialization - Defines the initialization. Performed before starting the loop iteration.
  • condition - Defines the condition. Performed before each loop iteration.
  • iterator - Defines the iterator. Performed after each loop iteration.
  • body - Defines the body. Performed in each loop iteration.

ExpressionEx.For does not declare the iteration variable. You may have to declare it using an Expression.Block.

Here's an example, using ExpressionEx.For, that calculates the sum of the items in an array:

using static NetFabric.Expressions.ExpressionEx;
using static System.Linq.Expressions.Expression;

int Sum(int[] array, int start, int end)
{
    var arrayParameter = Parameter(typeof(int[]), "array");
    var startParameter = Parameter(typeof(int), "start");
    var endParameter = Parameter(typeof(int), "end");
    var indexVariable = Variable(typeof(int), "index");
    var sumVariable = Variable(typeof(int), "sum");
    var expression = Block(
        new[] { indexVariable, sumVariable },
        Assign(sumVariable, Constant(0)),
        For(
            Assign(indexVariable, startParameter), 
            LessThan(indexVariable, endParameter), 
            PostIncrementAssign(indexVariable),
            AddAssign(sumVariable, ArrayIndex(arrayParameter, indexVariable))),
        sumVariable);
    var sum = Lambda<Func<int[], int, int, int>>(expression, arrayParameter, startParameter, endParameter).Compile();

    return sum(array, start, end);
}

ExpressionEx.While

public static LoopExpression While(Expression condition, Expression body) 
  • condition - Defines the condition. Performed before each loop iteration.
  • body - Defines the body. Performed in each loop iteration.

Here's an example, using ExpressionEx.While, that calculates the sum of the items in an array:

using static NetFabric.Expressions.ExpressionEx;
using static System.Linq.Expressions.Expression;

int Sum(int[] array, int start, int end)
{
    var valueParameter = Parameter(typeof(int[]), "value");
    var startParameter = Parameter(typeof(int), "start");
    var endParameter = Parameter(typeof(int), "end");
    var sumVariable = Variable(typeof(int), "sum");
    var indexVariable = Variable(typeof(int), "index");
    var expression = Block(
        new[] { indexVariable, sumVariable },
        Assign(sumVariable, Constant(0)),
        Assign(indexVariable, startParameter),
        While(
            LessThan(indexVariable, endParameter), 
            Block(
                AddAssign(sumVariable, ArrayIndex(valueParameter, indexVariable)),
                PostIncrementAssign(indexVariable)
            )
        ),
        sumVariable);
    var sum = Lambda<Func<int[], int, int, int>>(expression, valueParameter, startParameter, endParameter).Compile();

    return sum(array, start, end);
}

ExpressionEx.Using

public static TryExpression Using(ParameterExpression instance, Expression body) 
  • instance - Defines the variable to be disposed.
  • body - Defines the body after which the variable is disposed.

Throws and exception if the variable is not disposable. To be considered disposable, if it's is a class or a struct, it has to implement the IDisposable interface. If it's a ref struct, it only needs to have a public parameterless Dispose.

ExpressionEx.Using does not declare the iteration variable. You may have to declare it using an Expression.Block.

WARNING: IAsyncDisposable is not supported.

Here's an example, using ExpressionEx.Using, that calculates the sum of the items in an enumerable:

using static NetFabric.Expressions.ExpressionEx;
using static System.Linq.Expressions.Expression;

int Sum<TEnumerable>(TEnumerable enumerable)
{
    if (!typeof(TEnumerable).IsEnumerable(out var enumerableInfo))
        throw new Exception("Not an enumerable!");
    
    var enumerableParameter = Parameter(typeof(TEnumerable), "enumerable");
    var enumeratorVariable = Variable(enumerableInfo.GetEnumerator.ReturnType, "enumerator");
    var sumVariable = Variable(typeof(int), "sum");
    var expression = Block(
        new[] {enumeratorVariable, sumVariable},
        Assign(enumeratorVariable, Call(enumerableParameter, enumerableInfo.GetEnumerator)),
        Assign(sumVariable, Constant(0)),
        Using(
            enumeratorVariable,
            While(
                Call(enumeratorVariable, enumerableInfo.EnumeratorInfo.MoveNext),
                AddAssign(sumVariable, Call(enumeratorVariable, enumerableInfo.EnumeratorInfo.GetCurrent))
            )
        ),
        sumVariable);
    var sum = Lambda<Func<TEnumerable, int>>(expression, enumerableParameter).Compile();
    
    return sum(enumerable);
}
Product Compatible and additional computed target framework versions.
.NET net6.0 is compatible.  net6.0-android was computed.  net6.0-ios was computed.  net6.0-maccatalyst was computed.  net6.0-macos was computed.  net6.0-tvos was computed.  net6.0-windows was computed.  net7.0 was computed.  net7.0-android was computed.  net7.0-ios was computed.  net7.0-maccatalyst was computed.  net7.0-macos was computed.  net7.0-tvos was computed.  net7.0-windows was computed.  net8.0 was computed.  net8.0-android was computed.  net8.0-browser was computed.  net8.0-ios was computed.  net8.0-maccatalyst was computed.  net8.0-macos was computed.  net8.0-tvos was computed.  net8.0-windows was computed. 
Compatible target framework(s)
Included target framework(s) (in package)
Learn more about Target Frameworks and .NET Standard.
  • net6.0

    • No dependencies.

NuGet packages (1)

Showing the top 1 NuGet packages that depend on NetFabric.Reflection:

Package Downloads
NetFabric.Assertive

A fluent assertions library that performs full coverage on enumerable types.

GitHub repositories

This package is not used by any popular GitHub repositories.

Version Downloads Last updated
5.0.0 2,173 10/10/2023
4.2.1 549 9/16/2023
4.2.0 132 9/7/2023
4.1.0 473 7/14/2023
4.0.4 1,469 7/10/2021
4.0.3 407 7/10/2021
4.0.2 400 4/19/2021
4.0.1 762 4/16/2021
4.0.0 309 4/14/2021
3.2.1 320 3/31/2021
3.2.0 298 3/29/2021
3.1.0 342 3/26/2021
3.0.0 3,054 5/1/2020
2.0.1 483 4/29/2020
2.0.0 2,362 12/11/2019
1.1.0 583 12/5/2019
1.0.0 780 12/2/2019