Expression for retrieving a static field

suggest change

Given 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"

Feedback about page:

Feedback:
Optional: your email if you want me to get back to you:



Table Of Contents