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對象中找到的每個屬性的屬性信息?