summaryrefslogtreecommitdiff
path: root/Xamarin.Forms.Platform.WinRT/WindowsExpressionSearch.cs
blob: 778cc135b8578509de2e28471edc75e1b0d4b56e (plain)
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
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
´╗┐using System;
using System.Collections.Generic;
using System.Linq;
using System.Linq.Expressions;
using System.Reflection;

#if WINDOWS_UWP

namespace Xamarin.Forms.Platform.UWP
#else

namespace Xamarin.Forms.Platform.WinRT
#endif
{
	internal sealed class WindowsExpressionSearch : IExpressionSearch
	{
		List<object> _results;
		Type _targeType;

		public List<T> FindObjects<T>(Expression expression) where T : class
		{
			_results = new List<object>();
			_targeType = typeof(T);

			Visit(expression);

			List<T> final = _results.Cast<T>().ToList();
			_results = null;
			return final;
		}

		void Visit(Expression expression)
		{
			if (expression == null)
				return;

			switch (expression.NodeType)
			{
				case ExpressionType.Negate:
				case ExpressionType.NegateChecked:
				case ExpressionType.Not:
				case ExpressionType.Convert:
				case ExpressionType.ConvertChecked:
				case ExpressionType.ArrayLength:
				case ExpressionType.Quote:
				case ExpressionType.TypeAs:
				case ExpressionType.UnaryPlus:
					Visit(((UnaryExpression)expression).Operand);
					break;
				case ExpressionType.Add:
				case ExpressionType.AddChecked:
				case ExpressionType.Subtract:
				case ExpressionType.SubtractChecked:
				case ExpressionType.Multiply:
				case ExpressionType.MultiplyChecked:
				case ExpressionType.Divide:
				case ExpressionType.Modulo:
				case ExpressionType.Power:
				case ExpressionType.And:
				case ExpressionType.AndAlso:
				case ExpressionType.Or:
				case ExpressionType.OrElse:
				case ExpressionType.LessThan:
				case ExpressionType.LessThanOrEqual:
				case ExpressionType.GreaterThan:
				case ExpressionType.GreaterThanOrEqual:
				case ExpressionType.Equal:
				case ExpressionType.NotEqual:
				case ExpressionType.Coalesce:
				case ExpressionType.ArrayIndex:
				case ExpressionType.RightShift:
				case ExpressionType.LeftShift:
				case ExpressionType.ExclusiveOr:
					var binary = (BinaryExpression)expression;
					Visit(binary.Left);
					Visit(binary.Right);
					Visit(binary.Conversion);
					break;
				case ExpressionType.TypeIs:
					Visit(((TypeBinaryExpression)expression).Expression);
					break;
				case ExpressionType.Conditional:
					var conditional = (ConditionalExpression)expression;
					Visit(conditional.Test);
					Visit(conditional.IfTrue);
					Visit(conditional.IfFalse);
					break;
				case ExpressionType.MemberAccess:
					VisitMemberAccess((MemberExpression)expression);
					break;
				case ExpressionType.Call:
					var methodCall = (MethodCallExpression)expression;
					Visit(methodCall.Object);
					VisitList(methodCall.Arguments, Visit);
					break;
				case ExpressionType.Lambda:
					Visit(((LambdaExpression)expression).Body);
					break;
				case ExpressionType.New:
					VisitList(((NewExpression)expression).Arguments, Visit);
					break;
				case ExpressionType.NewArrayInit:
				case ExpressionType.NewArrayBounds:
					VisitList(((NewArrayExpression)expression).Expressions, Visit);
					break;
				case ExpressionType.Invoke:
					var invocation = (InvocationExpression)expression;
					VisitList(invocation.Arguments, Visit);
					Visit(invocation.Expression);
					break;
				case ExpressionType.MemberInit:
					var init = (MemberInitExpression)expression;
					VisitList(init.NewExpression.Arguments, Visit);
					VisitList(init.Bindings, VisitBinding);
					break;
				case ExpressionType.ListInit:
					var init1 = (ListInitExpression)expression;
					VisitList(init1.NewExpression.Arguments, Visit);
					VisitList(init1.Initializers, initializer => VisitList(initializer.Arguments, Visit));
					break;
				case ExpressionType.Constant:
					break;
				default:
					throw new ArgumentException(string.Format("Unhandled expression type: '{0}'", expression.NodeType));
			}
		}

		void VisitBinding(MemberBinding binding)
		{
			switch (binding.BindingType)
			{
				case MemberBindingType.Assignment:
					Visit(((MemberAssignment)binding).Expression);
					break;
				case MemberBindingType.MemberBinding:
					VisitList(((MemberMemberBinding)binding).Bindings, VisitBinding);
					break;
				case MemberBindingType.ListBinding:
					VisitList(((MemberListBinding)binding).Initializers, initializer => VisitList(initializer.Arguments, Visit));
					break;
				default:
					throw new ArgumentException(string.Format("Unhandled binding type '{0}'", binding.BindingType));
			}
		}

		static void VisitList<TList>(IEnumerable<TList> list, Action<TList> visitor)
		{
			foreach (TList element in list)
			{
				visitor(element);
			}
		}

		// All important magic happens here
		void VisitMemberAccess(MemberExpression member)
		{
			if (member.Expression is ConstantExpression && member.Member is FieldInfo)
			{
				object container = ((ConstantExpression)member.Expression).Value;
				object value = ((FieldInfo)member.Member).GetValue(container);

				if (_targeType.IsInstanceOfType(value))
				{
					_results.Add(value);
				}
			}
			Visit(member.Expression);
		}
	}
}