Expression for retrieving a static field
suggest changeGiven a type like this:
public class TestClass
{
public static string StaticPublicField = "StaticPublicFieldValue";
}
The value of StaticPublicField can be retrieved with an expression tree. Pass null as the instance, because the field is static:
var fieldExpr = Expression.Field(null, typeof(TestClass), "StaticPublicField");
var lambda = Expression.Lambda<Func<string>>(fieldExpr);
Func<string> retriever = lambda.Compile();
var fieldValue = retriever(); // "StaticPublicFieldValue"
Found a mistake? Have a question or improvement idea?
Let me know.
Table Of Contents