score:2
The lambda in your example has "closed over" the testValue
variable, meaning the compiler has captured it as a field of the same name in an automatically generated class called ConsoleApplication1.Program+<>c__DisplayClass1>
. You can use normal reflection to get the current value of that field by casting the right hand-side of the binary expression into a MemberExpression.
var testValue = "hello";
var expr = (Expression<Func<string, bool>>) (x => x == testValue);
var rhs = (MemberExpression) ((BinaryExpression) expr.Body).Right;
var obj = ((ConstantExpression) rhs.Expression).Value;
var field = (FieldInfo) rhs.Member;
var value = field.GetValue(obj);
Debug.Assert(Equals(value, "hello"));
testValue = "changed";
value = field.GetValue(obj);
Debug.Assert(Equals(value, "changed"));
Alternatively you can change your variable into a constant.
const string testValue = "hello";
var expr = (Expression<Func<string, bool>>) (x => x == testValue);
var value = ((ConstantExpression) ((BinaryExpression) expr.Body).Right).Value;
Debug.Assert(Equals(value, "hello"));
score:0
Instead of doing this by yourself, have a look at PartialEvaluator
from Matt Warren. It replaces all references to constants with the constants themselves.
Source: stackoverflow.com
Related Articles
- Expression Tree with string assignment and getting value
- Linq in conjunction with custom generic extension method build error - An expression tree may not contain an assignment operator?
- C#: Recursively get Dictonary value with deep string expression
- Building an Expression tree for Comparison Operations with value concatenated e.g ">= 1"
- Getting Nth value with Linq
- How Build Lambda Expression Tree with multiple conditions
- How do I create a Linq expression tree with an F# lambda?
- Can you reverse order a string in one line with LINQ or a LAMBDA expression
- Getting object with max date time value in one of it's properties
- Check if a String value contains any number by using Lambda Expression
- How to get a value out of a Span<T> with Linq expression trees?
- An expression tree may not contain an assignment operator?
- A lambda expression with a statement body cannot be converted to an expression tree in nopCommerce
- How to find key value pair in a dictionary with values > 0 with key matching a certain string pattern?
- Expression tree to initialize new anonymous object with arbitrary number of properties
- LINQ: Getting the row with the maximum value of a given attribute
- Build GroupBy expression tree with multiple fields
- Expression to get LINQ with Contains to EF for SQL IN() where on entities child's property equals value
- Getting average value of groups with LINQ
- Translating expression tree from a type to another type with complex mappings
- Creating a tree with LINQ expression
- Trim String value with particular pattern in C#.NET
- Replace parameter value in Expression Tree
- how to create a pivot table with dynamic column using linq tree expression
- How do I dynamically construct an Expression Tree to use with anonymous types
- Intersect two lists and return the similarity with the preserved order of the original first string value
- Split string and set default value if separator not found WITH LINQ
- The given value of type String from the data source cannot be converted to type int of the specified target column
- Expression Tree with Property Inheritance causes an argument exception
- Combine property selector expression tree and value to create a predicate for EF filtering - create filter from lambda selector and value
- Caching Method For Result String or Linq Query in Web Services C#
- Linq Group by won't work on multiple columns vb.net
- Linq Count() will always return 1 within Visual Studio
- linq checking that no value matches
- Exposing/Passing a LINQ expression to be used on an private list
- How do I get the objects from a list of objects which contain a duplicate property inside of another list in the object?
- Using Except() on a Generic collection
- LINQ to SQL update all records
- Why can't I use Linq Select in subclass of List<string> using base.Select?
- Linq statement for an infinite sequence of successive halves
- Grouping in Linq in a not so well organized xml
- How to reset id after deleting table using Linq in asp.net mvc
- Searching for dictionary keys contained in a string array
- DataContext Translate<> for anonymous types
- LINQ results matching ALL relationships
- ASP.net Core C# Linq Distinct not working
- Sequence contains no matching element error when a table is empty
- how to Filter one list based on another lists field
- Get value in xml from c#
- How can I speed up my Entity Framework LINQ Query?