using System.Linq.Expressions;
public static void Main()
var someClass = new SomeClass
PropertyThree = "Value3",
var props = new string[] {"PropertyOne", "PropertyTwo" };
Expression<Func<SomeClass, string>> keyFactoryExp = CreateExpression<SomeClass>(props);
keyFactoryExp.Compile().Invoke(someClass).Dump();
static Expression<Func<T, string>> CreateExpression<T>(string[] props)
var par = Expression.Parameter(typeof(T));
return Expression.Lambda<Func<T, string>>(Expression.Call(typeof(string).GetMethod("Join", new Type[] { typeof(string), typeof(object[]) }), Expression.Constant("|"),
Expression.NewArrayInit(typeof(object), props.Select(prop => Expression.Property(par, prop)).ToArray())), par);
public string PropertyOne { get; set; }
public string PropertyTwo { get; set; }
public string PropertyThree { get; set; }
public string PropertyFour { get; set; }