Dynamicaly Creating Expression Accessing a Property

/ Published in: C#
Save to your folder(s)

Creating the equivalent of (ParentType x) => x.SomeProperty when the property is unknown until runtime

Copy this code and paste it in your HTML
  1. var parameterExpression = Expression.Parameter(typeof(ParentType), "x");
  2. var memberExpression = Expression.Property(parameterExpression, propertyName);
  3. var result = (Expression<Func<ParentType, PropertyType>>)Expression.Lambda(memberExpression, parameterExpression);

Report this snippet


RSS Icon Subscribe to comments

You need to login to post a comment.