forked from bonsai-rx/expression-scripting
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathParsingConfigHelper.cs
More file actions
56 lines (49 loc) · 1.74 KB
/
ParsingConfigHelper.cs
File metadata and controls
56 lines (49 loc) · 1.74 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
using System;
using System.Collections.Generic;
using System.Linq;
using System.Linq.Dynamic.Core;
using System.Linq.Dynamic.Core.CustomTypeProviders;
namespace Bonsai.Scripting.Expressions
{
internal static class ParsingConfigHelper
{
public static ParsingConfig CreateParsingConfig(params Type[] additionalTypes)
{
var config = new ParsingConfig();
config.CustomTypeProvider = CreateCustomTypeProvider(config, additionalTypes);
return config;
}
static IDynamicLinqCustomTypeProvider CreateCustomTypeProvider(ParsingConfig config, params Type[] additionalTypes)
{
return new SimpleDynamicLinqCustomTypeProvider(
config,
additionalTypes.SelectMany(EnumerateTypeHierarchy).ToList());
}
static IEnumerable<Type> EnumerateTypeHierarchy(Type type)
{
var interfaces = type.GetInterfaces();
for (int i = 0; i < interfaces.Length; i++)
{
yield return interfaces[i];
}
while (type is not null)
{
yield return type;
type = type.BaseType;
}
}
class SimpleDynamicLinqCustomTypeProvider : DefaultDynamicLinqCustomTypeProvider
{
readonly HashSet<Type> customTypes;
public SimpleDynamicLinqCustomTypeProvider(ParsingConfig config, IList<Type> additionalTypes)
: base(config, additionalTypes, cacheCustomTypes: false)
{
customTypes = new(AdditionalTypes);
}
public override HashSet<Type> GetCustomTypes()
{
return customTypes;
}
}
}
}