samedi 14 septembre 2019

How to get access to any property or field using expression

I want to get value from property or field by name using expression. It is possible?

class MyClass
{
    public string Value {get;set;} = "testvalue";
}
// like this
var delegate = Expression.Lambda<Func<MyClass, string, string>>(/* some parameters*/ ).Compile();
var myClass = new MyClass();
var result = delegate.Invoke(myClass, "Value");
// result == "testvalue"





Aucun commentaire:

Enregistrer un commentaire