forked from reactiveui/ReactiveUI
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathExpressionRewriter.cs
More file actions
95 lines (83 loc) · 3.85 KB
/
ExpressionRewriter.cs
File metadata and controls
95 lines (83 loc) · 3.85 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
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
// Licensed to the .NET Foundation under one or more agreements.
// The .NET Foundation licenses this file to you under the MS-PL license.
// See the LICENSE file in the project root for more information.
using System;
using System.Collections.Generic;
using System.Linq;
using System.Linq.Expressions;
using System.Reflection;
using System.Text;
using System.Threading.Tasks;
namespace ReactiveUI
{
internal class ExpressionRewriter : ExpressionVisitor
{
public override Expression Visit(Expression node)
{
switch (node.NodeType) {
case ExpressionType.ArrayIndex:
return this.VisitBinary((BinaryExpression)node);
case ExpressionType.ArrayLength:
return this.VisitUnary((UnaryExpression)node);
case ExpressionType.Call:
return this.VisitMethodCall((MethodCallExpression)node);
case ExpressionType.Index:
return this.VisitIndex((IndexExpression)node);
case ExpressionType.MemberAccess:
return this.VisitMember((MemberExpression)node);
case ExpressionType.Parameter:
return this.VisitParameter((ParameterExpression)node);
case ExpressionType.Constant:
return this.VisitConstant((ConstantExpression)node);
case ExpressionType.Convert:
return this.VisitUnary((UnaryExpression)node);
default:
throw new NotSupportedException(string.Format("Unsupported expression type: '{0}'", node.NodeType));
}
}
protected override Expression VisitBinary(BinaryExpression node)
{
if (!(node.Right is ConstantExpression)) {
throw new NotSupportedException("Array index expressions are only supported with constants.");
}
Expression left = this.Visit(node.Left);
Expression right = this.Visit(node.Right);
// Translate arrayindex into normal index expression
return Expression.MakeIndex(left, left.Type.GetRuntimeProperty("Item"), new[] {right });
}
protected override Expression VisitUnary(UnaryExpression node)
{
if (node.NodeType == ExpressionType.ArrayLength) {
Expression expression = this.Visit(node.Operand);
//translate arraylength into normal member expression
return Expression.MakeMemberAccess(expression, expression.Type.GetRuntimeProperty("Length"));
} else if (node.NodeType == ExpressionType.Convert) {
return this.Visit(node.Operand);
} else {
return node.Update(this.Visit(node.Operand));
}
}
protected override Expression VisitMethodCall(MethodCallExpression node)
{
// Rewrite a method call to an indexer as an index expression
if (node.Arguments.Any(e => !(e is ConstantExpression)) || !node.Method.IsSpecialName) {
throw new NotSupportedException("Index expressions are only supported with constants.");
}
Expression instance = this.Visit(node.Object);
IEnumerable<Expression> arguments = this.Visit(node.Arguments);
// Translate call to get_Item into normal index expression
return Expression.MakeIndex(instance, instance.Type.GetRuntimeProperty("Item"), arguments);
}
protected override Expression VisitIndex(IndexExpression node)
{
if (node.Arguments.Any(e => !(e is ConstantExpression))) {
throw new NotSupportedException("Index expressions are only supported with constants.");
}
return base.VisitIndex(node);
}
protected override Expression VisitMember(MemberExpression node)
{
return base.VisitMember(node);
}
}
}