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

JaCraig / FastActivator / 21981925181

13 Feb 2026 09:36AM UTC coverage: 90.155% (-1.0%) from 91.192%
21981925181

push

github

web-flow
Merge pull request #331 from JaCraig/dependabot/nuget/FastActivator.Tests/dependencies-f78af83bdb

chore: Bump the dependencies group with 1 update

48 of 60 branches covered (80.0%)

Branch coverage included in aggregate %.

126 of 133 relevant lines covered (94.74%)

21.47 hits per line

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

87.3
/FastActivator/Utils/Constructor.cs
1
using System;
2
using System.Linq.Expressions;
3
using System.Reflection;
4

5
namespace Fast.Activator.Utils
6
{
7
    /// <summary>
8
    /// Constructor
9
    /// </summary>
10
    public class Constructor
11
    {
12
        /// <summary>
13
        /// Initializes a new instance of the <see cref="Constructor"/> class.
14
        /// </summary>
15
        /// <param name="constructor">The constructor.</param>
16
        /// <param name="parameters">The parameters.</param>
17
        /// <exception cref="ArgumentNullException">constructor</exception>
18
        public Constructor(ConstructorInfo constructor, ParameterInfo[] parameters)
13✔
19
        {
20
            ArgumentNullException.ThrowIfNull(constructor);
13✔
21

22
            parameters ??= [];
13!
23

24
            ParameterLength = parameters.Length;
13✔
25
            Parameters = new Type[ParameterLength];
13✔
26
            ParameterNullable = new bool[ParameterLength];
13✔
27

28
            for (var X = 0; X < ParameterLength; ++X)
56✔
29
            {
30
                Parameters[X] = parameters[X].ParameterType;
15✔
31
                ParameterNullable[X] = Nullable.GetUnderlyingType(Parameters[X]) is not null;
15✔
32
            }
33

34
            ConstructorDelegate = CreateConstructor(constructor, parameters);
13✔
35
        }
13✔
36

37
        /// <summary>
38
        /// Initializes a new instance of the <see cref="Constructor"/> class.
39
        /// </summary>
40
        /// <param name="delegate">The delegate.</param>
41
        public Constructor(ConstructorDelegate @delegate)
2✔
42
        {
43
            ConstructorDelegate = @delegate;
2✔
44
        }
2✔
45

46
        /// <summary>
47
        /// Gets the size of the parameter.
48
        /// </summary>
49
        /// <value>The size of the parameter.</value>
50
        public int ParameterLength { get; } = 0;
274✔
51

52
        /// <summary>
53
        /// Gets the constructor information.
54
        /// </summary>
55
        /// <value>The constructor information.</value>
56
        internal ConstructorDelegate ConstructorDelegate { get; }
64✔
57

58
        /// <summary>
59
        /// Gets the parameter nullable.
60
        /// </summary>
61
        /// <value>The parameter nullable.</value>
62
        private bool[] ParameterNullable { get; } = [];
30✔
63

64
        /// <summary>
65
        /// Gets the parameters.
66
        /// </summary>
67
        /// <value>The parameters.</value>
68
        private Type[] Parameters { get; } = [];
98✔
69

70
        /// <summary>
71
        /// Create an instance.
72
        /// </summary>
73
        /// <param name="args">The arguments.</param>
74
        /// <returns>The new object</returns>
75
        public object CreateInstance(object[] args) => ConstructorDelegate(args);
60✔
76

77
        /// <summary>
78
        /// Determines whether the specified arguments is a match.
79
        /// </summary>
80
        /// <param name="args">The arguments.</param>
81
        /// <returns><c>true</c> if the specified arguments is a match; otherwise, <c>false</c>.</returns>
82
        public bool IsMatch(object[] args)
83
        {
84
            if (args is null || args.Length != ParameterLength)
101✔
85
                return false;
51✔
86
            for (var X = 0; X < ParameterLength; ++X)
206✔
87
            {
88
                Type ArgType = args[X]?.GetType();
53!
89
                Type Parameter = Parameters[X];
53✔
90
                if (ArgType is null)
53!
91
                {
92
                    if (ParameterNullable[X])
×
93
                        continue;
94
                    return false;
×
95
                }
96
                if (!Parameter.IsAssignableFrom(ArgType))
53!
97
                    return false;
×
98
            }
99
            return true;
50✔
100
        }
101

102
        /// <summary>
103
        /// Creates the argument expression.
104
        /// </summary>
105
        /// <param name="parameterExpression">The parameter expression.</param>
106
        /// <param name="parameterInfo">The parameter information.</param>
107
        /// <param name="index">The index.</param>
108
        /// <returns>The argument expression</returns>
109
        private static UnaryExpression CreateArgumentExpression(ParameterExpression parameterExpression, ParameterInfo parameterInfo, int index)
110
        {
111
            return Expression.Convert(
15✔
112
                Expression.ArrayIndex(parameterExpression, Expression.Constant(index)),
15✔
113
                parameterInfo.ParameterType);
15✔
114
        }
115

116
        /// <summary>
117
        /// Creates the constructor.
118
        /// </summary>
119
        /// <param name="constructor">The constructor.</param>
120
        /// <param name="parameters">The parameters.</param>
121
        /// <returns>The constructor</returns>
122
        private static ConstructorDelegate CreateConstructor(ConstructorInfo constructor, ParameterInfo[] parameters)
123
        {
124
            ParameterExpression ParameterExpression = Expression.Parameter(typeof(object[]), "args");
13✔
125

126
            var ArgsExpressions = new Expression[parameters.Length];
13✔
127
            for (var X = 0; X < parameters.Length; ++X)
56✔
128
            {
129
                ArgsExpressions[X] = CreateArgumentExpression(ParameterExpression, parameters[X], X);
15✔
130
            }
131

132
            Expression NewExpression = Expression.New(constructor, ArgsExpressions);
13✔
133
            if (constructor.DeclaringType.IsValueType)
13✔
134
                NewExpression = Expression.Convert(NewExpression, typeof(object));
1✔
135

136
            return Expression.Lambda<ConstructorDelegate>(NewExpression, ParameterExpression).Compile();
13✔
137
        }
138
    }
139
}
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

© 2026 Coveralls, Inc