NetFabric.Reflection
4.2.1
Prefix Reserved
See the version list below for details.
dotnet add package NetFabric.Reflection --version 4.2.1
NuGet\Install-Package NetFabric.Reflection -Version 4.2.1
<PackageReference Include="NetFabric.Reflection" Version="4.2.1" />
paket add NetFabric.Reflection --version 4.2.1
#r "nuget: NetFabric.Reflection, 4.2.1"
// Install NetFabric.Reflection as a Cake Addin #addin nuget:?package=NetFabric.Reflection&version=4.2.1 // Install NetFabric.Reflection as a Cake Tool #tool nuget:?package=NetFabric.Reflection&version=4.2.1
NetFabric.Reflection
This package extends the reflection API.
Enumerable type checking
To find if a type is enumerable, it's not enough to check if it implements IEnumerable
, IEnumerable<>
or IAsyncEnumerable<>
. The foreach
and await foreach
statements support several other cases.
NOTE: Check the article "Efficient Data Processing: Leveraging C#'s foreach Loop" to understand all the possible cases supported by the
foreach
statement.
This package provides extension methods for the type Type
that can correctly validate if the type it represents can be used as the source in foreach
or await foreach
statements.
IsEnumerable and IsEnumerator
public static bool IsEnumerable(this Type type,
[NotNullWhen(true)] out EnumerableInfo? enumerableInfo,
out Errors errors);
public static bool IsEnumerator(this Type type,
out Errors errors);
The methods return true
if the type represented by Type
can be used in a foreach
statement; otherwise false
.
NOTE: It does not support the case when
GetEnumerator()
is defined as an extension method. It's not possible to find extension methods using reflection.
IsEnumerable()
calls IsEnumerator()
internally to validate the type returned by GetEnumerator()
. The method IsEnumerable()
only returns true
if both the enumerable and the enumerator are valid.
If the method IsEnumerable()
returns true
, the enumerableInfo
output parameter contains all the MethodInfo
and PropertySymbol
for the methods and properties that are going to be actually used by the foreach
statement. The GetEnumerator()
of the enumerable, the property Current
and the method MoveNext()
of the enumerator. It may also contain info for methods Reset()
and Dispose()
of the enumerator, if defined.
Is the methods return false
, the errors
output parameter indicates why the type is not considered an enumerable. It can be a combination of Error.MissingGetEnumerator
, Error.MissingCurrent
and Error.MissingMoveNext
.
The output parameter also includes a ForEachUsesIndexer
boolean property that indicates that, although the collection provides an enumerator, foreach
will use the indexer instead. That's the case for arrays and spans.
You can use these info values to further validate the enumerable and its respective enumerator. For example, use the following to find if the Current
property of the enumerator returns by reference:
enumerableInfo.EnumeratorSymbols.Current.ReturnsByRef;
IsAsyncEnumerable and IsAsyncEnumerator
public static bool IsAsyncEnumerable(this Type type,
[NotNullWhen(true)] out AsyncEnumerableInfo? enumerableInfo,
out Errors errors);
public static bool IsAsyncEnumerator(this Type type,
out Errors errors)
The methods return true
if the type represented by Type
can be used in an await foreach
statement; otherwise false
.
NOTE: It does not support the case when
GetAsyncEnumerator()
is defined as an extension method. It's not possible to find extension methods using reflection.
IsAsyncEnumerable()
calls IsAsyncEnumerator()
internally to validate the type returned by GetAsyncEnumerator()
. The method IsAsyncEnumerable()
only returns true
if both the enumerable and the enumerator are valid.
If the method IsAsyncEnumerable()
returns true
, the enumerableInfo
output parameter contains all the MethodInfo
and PropertySymbol
for the methods and properties that are going to be actually used by the await foreach
statement. The GetAsyncEnumerator()
of the enumerable, the property Current
and the method MoveNextAsync()
of the enumerator. It may also contain info for method DisposeAsync()
of the enumerator, if defined.
Is the methods return false
, the errors
output parameter indicates why the type is not considered an enumerable. It can be a combination of Error.MissingGetEnumerator
, Error.MissingCurrent
and Error.MissingMoveNext
.
You can use these info values to further validate the async enumerable or its respective enumerator.
Expression trees
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 anExpression
that defines an item, returns anExpression
that uses it.
WARNING: Async enumerables are not supported.
The Expression
generated depends on:
- Whether the enumerator is an
interface
,class
,struct
, orref 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 | Versions 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. |
-
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 |
Various small fixes