0
我需要更改一个函数,该函数接受一个带有一个属性的Expression,并使其具有至少处理两个属性的能力。从Expression对象中找到的多个属性对象获取PropertyInfo
我有一个包含嵌套类ElementHelper
public class DomainObjectViewModel<TModel> where TModel : DomainObject
{
public class ElementHelper
{
public static void Create<T1>(TModel model, Expression<Func<TModel, T1>> expression)
{
var getPropertyInfo = GetPropertyInfo(expression);
//Do cool stuff
}
private static PropertyInfo GetPropertyInfo<T1>(Expression<Func<TModel, T1>> propertyExpression)
{
return (PropertyInfo)((MemberExpression)propertyExpression.Body).Member;
}
}
}
-ElementHelper类包含了创建功能得到表达的PropertyInfo且仅当你在表达传递一个物业工作的下面的基类。
然后我有下面的继承类,在构造函数中使用助手函数。
public class ProductViewModel : DomainObjectViewModel<ProductEditViewModel>
{
public ProductViewModel(ProductEditViewModel model)
{
//It works for one property in the Expression
ElementHelper.Create(model, x => x.LaunchDate);
//Give the ability to pass multiple paramenters in the expression
ElementHelper.Create(model, x => new { x.LaunchDate, x.ApplyLaunchDateChanges });
}
}
我想我可以以通过它的属性的集合使用NewExpression(新{x.LaunchDate,x.ApplyLaunchDateChanges}),但我不能使它发挥作用。
你会用同样的方法吗?
如何拆分传递的表达式,以便获取NewExpression对象中找到的每个属性的属性信息?