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

microsoft / RulesEngine / 13046124752

30 Jan 2025 05:18AM UTC coverage: 93.976% (-0.3%) from 94.26%
13046124752

push

github

web-flow
Update projects for .NET 8.0 and 9.0 support (#648)

* Update projects for .NET 8.0 and 9.0 support

- Added BenchmarkDotNet attributes in Program.cs for .NET 8.0 and 9.0.
- Updated RulesEngineBenchmark.csproj to target .NET 8.0 and 9.0; upgraded BenchmarkDotNet to 0.14.0.
- Modified DemoApp.EFDataExample.csproj to target .NET 8.0 and 9.0; upgraded EF Core packages to 9.0.1.
- Changed DemoApp.csproj to target .NET 8.0 and 9.0, preserving project references and workflow files.
- Updated global.json to specify SDK version 9.0.0.
- Modified RulesEngine.csproj to target .NET 9.0 and updated package references, including System.Text.Json.
- Updated RulesEngine.UnitTest.csproj to target .NET 8.0 and 9.0; upgraded testing libraries and System.Text.Json.

* re-introduced net6.0 support

* update AutoFixture to fix vulnerabilities

* add JsonElement support to fix error in the DemoApp

---------

Co-authored-by: Purunjay Bhal <purunjaybhal@microsoft.com>

302 of 368 branches covered (82.07%)

18 of 20 new or added lines in 1 file covered. (90.0%)

624 of 664 relevant lines covered (93.98%)

312.68 hits per line

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

95.92
/src/RulesEngine/HelperFunctions/Utils.cs
1
// Copyright (c) Microsoft Corporation.
2
// Licensed under the MIT License.
3

4
using System;
5
using System.Collections;
6
using System.Collections.Generic;
7
using System.Dynamic;
8
using System.Linq;
9
using System.Linq.Dynamic.Core;
10

11
namespace RulesEngine.HelperFunctions
12
{
13
    public static class Utils
14
    {
15
        public static object GetTypedObject(dynamic input)
16
        {
17
            if (input is ExpandoObject)
840✔
18
            {
19
                Type type = CreateAbstractClassType(input);
294✔
20
                return CreateObject(type, input);
294✔
21
            }
22
            else
23
            {
24
                return input;
546✔
25
            }
26
        }
27
        public static Type CreateAbstractClassType(dynamic input)
28
        {
29
            List<DynamicProperty> props = [];
1,188✔
30

31
            if (input is System.Text.Json.JsonElement jsonElement)
1,188!
32
            {
NEW
33
                if (jsonElement.ValueKind == System.Text.Json.JsonValueKind.Null)
×
34
                {
NEW
35
                    return typeof(object);
×
36
                }
37
            }
38
            else if (input == null)
1,188✔
39
            {
40
                return typeof(object);
3✔
41
            }
42

43
            if (input is not ExpandoObject expandoObject)
1,185✔
44
            {
45
                return input.GetType();
882✔
46
            }
47

48
            foreach (var expando in expandoObject)
2,394✔
49
            {
50
                Type value;
51
                if (expando.Value is IList list)
894✔
52
                {
53
                    if (list.Count == 0)
15✔
54
                    {
55
                        value = typeof(List<object>);
3✔
56
                    }
57
                    else
58
                    {
59
                        var internalType = CreateAbstractClassType(list[0]);
12✔
60
                        value = new List<object>().Cast(internalType).ToList(internalType).GetType();
12✔
61
                    }
62

63
                }
64
                else
65
                {
66
                    value = CreateAbstractClassType(expando.Value);
879✔
67
                }
68
                props.Add(new DynamicProperty(expando.Key, value));
894✔
69
            }
70

71
            var type = DynamicClassFactory.CreateType(props);
303✔
72
            return type;
303✔
73
        }
74

75
        public static object CreateObject(Type type, dynamic input)
76
        {
77
            if (input is not ExpandoObject expandoObject)
339✔
78
            {
79
                return Convert.ChangeType(input, type);
36✔
80
            }
81
            var obj = Activator.CreateInstance(type);
303✔
82

83
            var typeProps = type.GetProperties().ToDictionary(c => c.Name);
1,494✔
84

85
            foreach (var expando in expandoObject)
2,388✔
86
            {
87
                if (typeProps.ContainsKey(expando.Key) &&
891!
88
                    expando.Value != null && (expando.Value.GetType().Name != "DBNull" || expando.Value != DBNull.Value))
891✔
89
                {
90
                    object val;
91
                    var propInfo = typeProps[expando.Key];
888✔
92
                    if (expando.Value is ExpandoObject)
888✔
93
                    {
94
                        var propType = propInfo.PropertyType;
6✔
95
                        val = CreateObject(propType, expando.Value);
6✔
96
                    }
97
                    else if (expando.Value is IList temp)
882✔
98
                    {
99
                        var internalType = propInfo.PropertyType.GenericTypeArguments.FirstOrDefault() ?? typeof(object);
12!
100
                        var newList = new List<object>().Cast(internalType).ToList(internalType);
12✔
101
                        foreach (var t in temp)
96✔
102
                        {
103
                            var child = CreateObject(internalType, t);
36✔
104
                            newList.Add(child);
36✔
105
                        };
106
                        val = newList;
12✔
107
                    }
108
                    else
109
                    {
110
                        val = expando.Value;
870✔
111
                    }
112
                    propInfo.SetValue(obj, val, null);
888✔
113
                }
114
            }
115

116
            return obj;
303✔
117
        }
118

119
        private static IEnumerable Cast(this IEnumerable self, Type innerType)
120
        {
121
            var methodInfo = typeof(Enumerable).GetMethod("Cast");
24✔
122
            var genericMethod = methodInfo.MakeGenericMethod(innerType);
24✔
123
            return genericMethod.Invoke(null, new[] { self }) as IEnumerable;
24✔
124
        }
125

126
        private static IList ToList(this IEnumerable self, Type innerType)
127
        {
128
            var methodInfo = typeof(Enumerable).GetMethod("ToList");
24✔
129
            var genericMethod = methodInfo.MakeGenericMethod(innerType);
24✔
130
            return genericMethod.Invoke(null, new[] { self }) as IList;
24✔
131
        }
132
    }
133

134

135
}
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