< Summary

Information
Class: Elsa.Dsl.ElsaScript.Helpers.ActivityActivator
Assembly: Elsa.Dsl.ElsaScript
File(s): /home/runner/work/elsa-core/elsa-core/src/modules/Elsa.Dsl.ElsaScript/Helpers/ActivityActivator.cs
Line coverage
94%
Covered lines: 18
Uncovered lines: 1
Coverable lines: 19
Total lines: 43
Line coverage: 94.7%
Branch coverage
70%
Covered branches: 7
Total branches: 10
Branch coverage: 70%
Method coverage

Feature is only available for sponsors

Upgrade to PRO version

Metrics

MethodBranch coverage Crap Score Cyclomatic complexity Line coverage
Create(...)70%101094.73%

File(s)

/home/runner/work/elsa-core/elsa-core/src/modules/Elsa.Dsl.ElsaScript/Helpers/ActivityActivator.cs

#LineLine coverage
 1using Elsa.Workflows;
 2
 3namespace Elsa.Dsl.ElsaScript.Helpers;
 4
 5internal static class ActivityActivator
 6{
 7    public static IActivity Create(Type type)
 8    {
 9        // Try parameterless ctor first (fast path)
 210        var parameterless = type.GetConstructor(Type.EmptyTypes);
 211        if (parameterless != null)
 012            return (IActivity)parameterless.Invoke([]);
 13
 14        // Fall back: pick a public ctor and provide default values for args
 215        var ctor = type
 216                       .GetConstructors()
 1417                       .OrderBy(c => c.GetParameters().Length) // shortest first
 218                       .FirstOrDefault()
 219                   ?? throw new InvalidOperationException(
 220                       $"Type {type} has no public constructors and cannot be instantiated.");
 21
 222        var parameters = ctor.GetParameters();
 223        var args = new object?[parameters.Length];
 24
 1625        for (var i = 0; i < parameters.Length; i++)
 26        {
 627            var p = parameters[i];
 28
 629            if (p.HasDefaultValue)
 30            {
 431                args[i] = p.DefaultValue;
 32            }
 33            else
 34            {
 235                args[i] = p.ParameterType.IsValueType
 236                    ? Activator.CreateInstance(p.ParameterType)
 237                    : null;
 38            }
 39        }
 40
 241        return (IActivity)ctor.Invoke(args);
 42    }
 43}

Methods/Properties

Create(System.Type)