• Home
  • Features
  • Pricing
  • Docs
  • Announcements
  • Sign In

Sholtee / injector / 2251

27 Dec 2023 10:00AM UTC coverage: 90.723% (-2.0%) from 92.704%
2251

push

appveyor

Sholtee
Merge branch 'drop_opencover'

2171 of 2393 relevant lines covered (90.72%)

0.91 hits per line

Source File
Press 'n' to go to next uncovered line, 'b' for previous

82.35
/SRC/Injector/Private/Expressions/Visitors/UnfoldLambdaExpressionVisitor.cs
1
/********************************************************************************
2
* UnfoldLambdaExpressionVisitor.cs                                              *
3
*                                                                               *
4
* Author: Denes Solti                                                           *
5
********************************************************************************/
6
using System;
7
using System.Collections.ObjectModel;
8
using System.Diagnostics;
9
using System.Linq.Expressions;
10

11
namespace Solti.Utils.DI.Internals
12
{
13
    using Primitives.Patterns;
14
    using Properties;
15

16
    /// <summary>
17
    /// Extracts the method body and replaces the parameters in the referencies.
18
    /// </summary>
19
    /// <remarks><code>param => param.DoComething();</code> becomes <code>substitution.DoSomething();</code></remarks>
20
    internal sealed class UnfoldLambdaExpressionVisitor : ExpressionVisitor
21
    {
22
        private readonly WriteOnce<ReadOnlyCollection<ParameterExpression>> FParameters = new();
1✔
23

24
        private readonly Expression[] FParameterSubstitutions;
25

26
        private UnfoldLambdaExpressionVisitor(Expression[] parameterSubstitutions) =>
1✔
27
            FParameterSubstitutions = parameterSubstitutions;
1✔
28

29
        public static Expression Unfold(LambdaExpression lamda, params Expression[] parameterSubstitutions) =>
30
            new UnfoldLambdaExpressionVisitor(parameterSubstitutions).Visit(lamda);
1✔
31

32
        protected override Expression VisitLambda<T>(Expression<T> lambda)
33
        {
1✔
34
            if (FParameters.HasValue)
1✔
35
                //
36
                // In nested lambdas we just replace the captured compatible variables.
37
                //
38

39
                return base.VisitLambda(lambda);
×
40

41
            if (lambda.Parameters.Count != FParameterSubstitutions.Length)
1✔
42
                throw new NotSupportedException(Resources.LAMBDA_LAYOUT_NOT_SUPPORTED);
×
43

44
            FParameters.Value = lambda.Parameters;
1✔
45

46
            //
47
            // From the main method we just need the method body
48
            //
49

50
            return Visit(lambda.Body);
1✔
51
        }
1✔
52

53
        protected override Expression VisitParameter(ParameterExpression parameter)
54
        {
1✔
55
            Debug.Assert(FParameters.HasValue, "Parameter list has not been determined");
1✔
56

57
            //
58
            // Find the corresponding parameter
59
            //
60

61
            int index = FParameters.Value!.IndexOf(parameter);
1✔
62
            return index >= 0 ? FParameterSubstitutions[index] : parameter;
×
63
        }
1✔
64
    }
65
}
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2025 Coveralls, Inc