Below is a simple demonstration code of my problem.
[TestClass]
public class ExpressionTests
{
[TestMethod]
public void TestParam()
{
Search<Student>(s => s.Id == 1L);
GetStudent(1L);
}
private void GetStudent(long id)
{
Search<Student>(s => s.Id == id);
}
private void Search<T>(Expression<Func<T, bool>> filter)
{
var visitor = new MyExpressionVisitor();
visitor.Visit(filter);
}
}
public class MyExpressionVisitor : ExpressionVisitor
{
protected override Expression VisitConstant(ConstantExpression node)
{
Assert.AreEqual(1L, node.Value);
return base.VisitConstant(node);
}
}
TestParam
method causes VisitConstant
to be invoked on two different paths:
1. TestParam
-> Search
-> VisitConstant
In this execution path constant expression (1L) passed to Search
method is a real constant value. Here, everything is OK, assert succeeds as expected. When VisitConstant
is invoked via first path node.Value.GetType()
is Int64
and its .Value
is 1L
.
2. TestParam
-> GetStudent
-> Search
-> VisitConstant
In this execution path constant expression (id: 1L), is taken by GetStudent
as an argument and passed to Search
method inside a closure.
Problem
The problem is on the second execution path. When VisitConstant
is invoked via second path node.Value.GetType()
is MyProject.Tests.ExpressionTests+<>c__DisplayClass0
and this class has a public field named id
(same as GetStudent
method's argument) which has the value of 1L
.
Question
How can I get id
value in second path? I know about closures, what a DisplayClass
is and why it is created at compile time etc. I am only interested in getting its field value.
One thing I can think of is, via reflection. With something like below but it does not seem neat.
node.Value.GetType().GetFields()[0].GetValue(node.Value);
Bonus Problem
While playing with the code for gettting id
value I changed VisitConstant
method like below (which will not solve my problem though) and get an exception saying "'object' does not contain a definition for 'id'"
Bonus Question
As dynamics are resolved at runtime and DisplayClass
is created at compile time, why cannot we access its fields with dynamic
? While below code works, I expected that code would work too.
var st = new {Id = 1L};
object o = st;
dynamic dy = o;
Assert.AreEqual(1L, dy.Id);