-
Notifications
You must be signed in to change notification settings - Fork 1
/
DictionaryToSwitchCaseGenerator.cs
78 lines (63 loc) · 3.22 KB
/
DictionaryToSwitchCaseGenerator.cs
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
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
using System;
using System.Collections.Generic;
using System.Linq;
using System.Linq.Expressions;
namespace Nucs.Reflection;
public class DictionaryToSwitchCaseGenerator {
/// <summary>
/// Creates a lookup via switch-case to given <paramref name="dictionary"/>
/// </summary>
public static Func<TKey, TValue> CreateLookupFunc<TKey, TValue>(IDictionary<TKey, TValue> dictionary) {
return CreateLookupExpression(dictionary).Compile();
}
/// <summary>
/// Creates a lookup via switch-case to given <paramref name="dictionary"/> for TValue of TKey
/// </summary>
public static Func<TValue, TKey> CreateReversedLookupFunc<TKey, TValue>(IDictionary<TKey, TValue> dictionary) {
return CreateReversedLookupExpression(dictionary).Compile();
}
/// <summary>
/// Creates a lookup via switch-case to given <paramref name="dictionary"/>
/// </summary>
public static Expression<Func<TKey, TValue>> CreateLookupExpression<TKey, TValue>(IDictionary<TKey, TValue> dictionary) {
// Create a parameter for the TValue value
var valueParam = Expression.Parameter(typeof(TKey), "value");
// Create a list of switch cases
var cases = dictionary.Select(kvp =>
Expression.SwitchCase(
Expression.Constant(kvp.Value),
Expression.Constant(kvp.Key)
)
);
// Create a default case
var defaultCase = Expression.Default(typeof(TValue));
// Create the switch expression
var switchExpr = Expression.Switch(valueParam, defaultCase, cases.ToArray());
// Create a lambda expression from the switch expression and the value parameter
var lambdaExpr = Expression.Lambda<Func<TKey, TValue>>(switchExpr, valueParam);
// Compile the lambda expression to a delegate and return it
return lambdaExpr;
}
/// <summary>
/// Creates a lookup via switch-case to given <paramref name="dictionary"/> for TValue of TKey
/// </summary>
public static Expression<Func<TValue, TKey>> CreateReversedLookupExpression<TKey, TValue>(IDictionary<TKey, TValue> dictionary) {
// Create a parameter for the TValue value
var valueParam = Expression.Parameter(typeof(TValue), "value");
// Create a list of switch cases
var cases = dictionary.Select(kvp =>
Expression.SwitchCase(
Expression.Constant(kvp.Key),
Expression.Constant(kvp.Value)
)
);
// Create a default case
var defaultCase = Expression.Default(typeof(TKey));
// Create the switch expression
var switchExpr = Expression.Switch(valueParam, defaultCase, cases.ToArray());
// Create a lambda expression from the switch expression and the value parameter
var lambdaExpr = Expression.Lambda<Func<TValue, TKey>>(switchExpr, valueParam);
// Compile the lambda expression to a delegate and return it
return lambdaExpr;
}
}