< Summary - CoreWCF Coverage — PR #1766

Information
Class: CoreWCF.Security.NegotiateInternal.LambdaExpressionBuilder
Assembly: CoreWCF.Primitives
File(s): /home/runner/work/CoreWCF/CoreWCF/src/CoreWCF.Primitives/src/CoreWCF/Security/NegotiateInternal/LambdaExpressionBuilder.cs
Line coverage
100%
Covered lines: 37
Uncovered lines: 0
Coverable lines: 37
Total lines: 88
Line coverage: 100%
Branch coverage
100%
Covered branches: 12
Total branches: 12
Branch coverage: 100%
Method coverage

Feature is only available for sponsors

Upgrade to PRO version

Metrics

MethodBranch coverage Cyclomatic complexity NPath complexity Sequence coverage
.cctor()100%22100%
BuildFor(...)100%22100%
IsSpan(...)100%66100%

File(s)

/home/runner/work/CoreWCF/CoreWCF/src/CoreWCF.Primitives/src/CoreWCF/Security/NegotiateInternal/LambdaExpressionBuilder.cs

#LineLine coverage
 1// Licensed to the .NET Foundation under one or more agreements.
 2// The .NET Foundation licenses this file to you under the MIT license.
 3
 4using System;
 5using System.Collections.Generic;
 6using System.Linq;
 7using System.Linq.Expressions;
 8using System.Reflection;
 9
 10namespace CoreWCF.Security.NegotiateInternal
 11{
 12    /// <summary>
 13    /// Helper class to compile a lambda expression to invoke a method that has a
 14    /// span input with reflection.
 15    /// The span input are expected as array type.
 16    /// Example of generated lambda: (object instance, byte[] input) => ((TargetInternalType) instance).TargetMethod((Re
 17    /// </summary>
 18    internal static class LambdaExpressionBuilder
 19    {
 120        private static readonly MethodInfo s_asSpan = typeof(MemoryExtensions)
 121                .GetMethods()
 26922                .Single(m => m.Name == "AsSpan" && m.ToString() == "System.Span`1[T] AsSpan[T](T[])");
 23
 24        public static LambdaExpression BuildFor(Type targetType, MethodInfo targetMethod)
 25        {
 126            var _callParameters = new List<Expression>();
 127            var _lambdaParameters = new List<ParameterExpression>
 128            {
 129                Expression.Parameter(typeof(object))
 130            };
 31
 32            void AddParameter(Type parameterType, bool isOut)
 33            {
 234                if (IsSpan(parameterType, out var itemType))
 35                {
 136                    var parameter = Expression.Parameter(itemType.MakeArrayType());
 137                    MethodInfo asSpanMethod = s_asSpan.MakeGenericMethod(itemType);
 138                    UnaryExpression span = Expression.Convert(
 139                        Expression.Call(asSpanMethod, parameter),
 140                        parameterType);
 41
 142                    _callParameters.Add(span);
 143                    _lambdaParameters.Add(parameter);
 44                }
 45                else
 46                {
 147                    var parameter = Expression.Parameter(parameterType);
 148                    _callParameters.Add(parameter);
 149                    _lambdaParameters.Add(parameter);
 50                }
 151            }
 52
 653            foreach (var methodParameter in targetMethod.GetParameters())
 54            {
 255                AddParameter(
 256                    methodParameter.ParameterType,
 257                    methodParameter.IsOut);
 58            }
 59
 160            var instanceParameter = _lambdaParameters[0];
 161            var typedInstance = Expression.TypeAs(instanceParameter, targetType);
 62
 163            MethodCallExpression call = Expression.Call(
 164                typedInstance,
 165                targetMethod,
 166                _callParameters);
 67
 168            return Expression.Lambda(call, _lambdaParameters.AsEnumerable());
 69        }
 70
 71        private static bool IsSpan(Type type, out Type itemType)
 72        {
 273            bool isSpan = (type.FullName.StartsWith("System.Span", StringComparison.Ordinal) || type.FullName.StartsWith
 274                && type.GenericTypeArguments.Length == 1;
 75
 276            if (isSpan)
 77            {
 178                itemType = type.GenericTypeArguments[0];
 79            }
 80            else
 81            {
 182                itemType = null;
 83            }
 84
 185            return isSpan;
 86        }
 87    }
 88}