Skip to content

Commit c5afb16

Browse files
committed
Update backend to System.Linq.Dynamic.Core
System.Linq.Dynamic has been deprecated and also does not target modern .NET so we decided to update to the modern Dynamic LINQ package, which includes improved language syntax and is easier to extend with code analysis and auto-completion features.
1 parent 6b3d2ef commit c5afb16

File tree

5 files changed

+50
-4
lines changed

5 files changed

+50
-4
lines changed

src/Bonsai.Scripting.Expressions/Bonsai.Scripting.Expressions.csproj

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,6 @@
55
</PropertyGroup>
66
<ItemGroup>
77
<PackageReference Include="Bonsai.Core" Version="2.9.0" />
8-
<PackageReference Include="System.Linq.Dynamic" Version="1.0.7" />
8+
<PackageReference Include="System.Linq.Dynamic.Core" Version="1.6.7" />
99
</ItemGroup>
1010
</Project>

src/Bonsai.Scripting.Expressions/ExpressionCondition.cs

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
using System.Collections.Generic;
44
using System.ComponentModel;
55
using System.Linq;
6+
using System.Linq.Dynamic.Core;
67
using System.Linq.Expressions;
78
using System.Reactive.Linq;
89
using System.Reflection;
@@ -52,7 +53,8 @@ public override Expression Build(IEnumerable<Expression> arguments)
5253
{
5354
var source = arguments.First();
5455
var sourceType = source.Type.GetGenericArguments()[0];
55-
var predicate = System.Linq.Dynamic.DynamicExpression.ParseLambda(sourceType, typeof(bool), Expression);
56+
var config = ParsingConfigHelper.CreateParsingConfig(sourceType);
57+
var predicate = DynamicExpressionParser.ParseLambda(config, sourceType, typeof(bool), Expression);
5658
return System.Linq.Expressions.Expression.Call(whereMethod.MakeGenericMethod(sourceType), source, predicate);
5759
}
5860

src/Bonsai.Scripting.Expressions/ExpressionSink.cs

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
using System.Collections.Generic;
44
using System.ComponentModel;
55
using System.Linq;
6+
using System.Linq.Dynamic.Core;
67
using System.Linq.Expressions;
78
using System.Reactive.Linq;
89
using System.Reflection;
@@ -59,7 +60,8 @@ public override Expression Build(IEnumerable<Expression> arguments)
5960
var sourceType = source.Type.GetGenericArguments()[0];
6061
var actionType = System.Linq.Expressions.Expression.GetActionType(sourceType);
6162
var itParameter = new[] { System.Linq.Expressions.Expression.Parameter(sourceType, string.Empty) };
62-
var onNext = System.Linq.Dynamic.DynamicExpression.ParseLambda(actionType, itParameter, null, Expression);
63+
var config = ParsingConfigHelper.CreateParsingConfig(sourceType);
64+
var onNext = DynamicExpressionParser.ParseLambda(actionType, config, itParameter, null, Expression);
6365
return System.Linq.Expressions.Expression.Call(doMethod.MakeGenericMethod(sourceType), source, onNext);
6466
}
6567
else return source;

src/Bonsai.Scripting.Expressions/ExpressionTransform.cs

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
using System.Collections.Generic;
44
using System.ComponentModel;
55
using System.Linq;
6+
using System.Linq.Dynamic.Core;
67
using System.Linq.Expressions;
78
using System.Reactive.Linq;
89
using System.Reflection;
@@ -53,7 +54,8 @@ public override Expression Build(IEnumerable<Expression> arguments)
5354
{
5455
var source = arguments.First();
5556
var sourceType = source.Type.GetGenericArguments()[0];
56-
var selector = System.Linq.Dynamic.DynamicExpression.ParseLambda(sourceType, null, Expression);
57+
var config = ParsingConfigHelper.CreateParsingConfig(sourceType);
58+
var selector = DynamicExpressionParser.ParseLambda(config, sourceType, null, Expression);
5759
return System.Linq.Expressions.Expression.Call(selectMethod.MakeGenericMethod(sourceType, selector.ReturnType), source, selector);
5860
}
5961

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
using System;
2+
using System.Collections.Generic;
3+
using System.Linq;
4+
using System.Linq.Dynamic.Core;
5+
using System.Linq.Dynamic.Core.CustomTypeProviders;
6+
7+
namespace Bonsai.Scripting.Expressions
8+
{
9+
internal static class ParsingConfigHelper
10+
{
11+
public static ParsingConfig CreateParsingConfig(params Type[] additionalTypes)
12+
{
13+
var config = new ParsingConfig();
14+
config.CustomTypeProvider = CreateCustomTypeProvider(config, additionalTypes);
15+
return config;
16+
}
17+
18+
static IDynamicLinqCustomTypeProvider CreateCustomTypeProvider(ParsingConfig config, params Type[] additionalTypes)
19+
{
20+
return new DefaultDynamicLinqCustomTypeProvider(
21+
config,
22+
additionalTypes.SelectMany(EnumerateTypeHierarchy).ToList());
23+
}
24+
25+
static IEnumerable<Type> EnumerateTypeHierarchy(Type type)
26+
{
27+
var interfaces = type.GetInterfaces();
28+
for (int i = 0; i < interfaces.Length; i++)
29+
{
30+
yield return interfaces[i];
31+
}
32+
33+
while (type is not null)
34+
{
35+
yield return type;
36+
type = type.BaseType;
37+
}
38+
}
39+
}
40+
}

0 commit comments

Comments
 (0)