-
-
Notifications
You must be signed in to change notification settings - Fork 802
/
EvaluateCaptures.cs
35 lines (31 loc) · 973 Bytes
/
EvaluateCaptures.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
// Copyright (c) 2007, Clarius Consulting, Manas Technology Solutions, InSTEDD, and Contributors.
// All rights reserved. Licensed under the BSD 3-Clause License; see License.txt.
using System.Linq.Expressions;
using System.Reflection;
using System.Runtime.CompilerServices;
namespace Moq.Expressions.Visitors
{
/// <summary>
/// Evaluates variables that have been closed over by a lambda function.
/// </summary>
internal sealed class EvaluateCaptures : ExpressionVisitor
{
public static readonly ExpressionVisitor Rewriter = new EvaluateCaptures();
private EvaluateCaptures()
{
}
protected override Expression VisitMember(MemberExpression node)
{
if (node.Member is FieldInfo fi
&& node.Expression is ConstantExpression ce
&& node.Member.DeclaringType.IsDefined(typeof(CompilerGeneratedAttribute)))
{
return Expression.Constant(fi.GetValue(ce.Value), node.Type);
}
else
{
return base.VisitMember(node);
}
}
}
}