我有一个类型的现有表达Expression<Func<T, object>>
; 它包含像cust => cust.Name
.
我还有一个带有类型字段的父类T
.我需要一个接受上面作为参数的方法,并生成一个新的表达式,将父类(TModel
)作为参数.这将用作MVC方法的表达式参数.
因此,cust => cust.Name
成为parent => parent.Customer.Name
.
同样,cust => cust.Address.State
成为parent => parent.Customer.Address.State
.
这是我的初始版本:
//note: the FieldDefinition object contains the first expression
//described above, plus the MemberInfo object for the property/field
//in question
public Expression<Func<TModel, object>> ExpressionFromField<TModel>(FieldDefinition<T> field)
where TModel: BaseModel<T>
{
var param = Expression.Parameter(typeof(TModel), "t");
//Note in the next line "nameof(SelectedItem)". This is a reference
//to the property in …
Run Code Online (Sandbox Code Playgroud)