因此,我正在嘗試使用atm表達式。得到以下代碼:除以下幾點外,代碼工作正常:我需要用ForeignKeyProperty.PropertyType替換ViewModel類型,該類僅在運行時在var condition = Expression.Lambda < Func < ViewModel, bool> >預期的最終結果:ForeignKeyProperty.SetValue(model, repository.GetList <ForeignKeyProperty.PropertyType >().Single(x => x.Id == model.Id));protected List < Action < IVenturaRepository, ViewModel>> SetForeignKeyProperties<ViewModel>() where ViewModel : BaseViewModel { var viewModelType = typeof(ViewModel); var foreignKeyProperties = viewModelType.GetProperties().Where(x => x.PropertyType.IsSubclassOf(typeof(BaseViewModel))); var actions = new List < Action < IVenturaRepository, ViewModel>>(); var repositoryType = typeof(IVenturaRepository); foreach(var ForeignKeyProperty in foreignKeyProperties) { var foreignKeyIdProperty = viewModelType.GetProperties().SingleOrDefault(x => x.Name == ForeignKeyProperty.Name + "Id"); //ForeignKeyProperty.SetValue(model, repository.GetList<ViewModel>().Single(x => x.Id == model.Id)); var listMethod = repositoryType.GetMethods().SingleOrDefault(x => x.Name == "GetList").MakeGenericMethod(ForeignKeyProperty.PropertyType); //Expression.Call(singleMethod,); var repositoryVariable = Expression.Parameter(repositoryType, "repository"); var paramViewModelType = Expression.Parameter(viewModelType, "model"); var paramForeignEntityId = Expression.Property(paramViewModelType, "Id"); var listMethodCall = Expression.Call(repositoryVariable, listMethod); var modelParameter = Expression.Parameter(ForeignKeyProperty.PropertyType, "x"); var foreignKeyTypeConstant = Expression.Constant(ForeignKeyProperty.PropertyType); ), modelParameter ); } return actions; }有人可以指出我正確的方向嗎?
- 2 回答
- 0 關注
- 145 瀏覽
添加回答
舉報
0/150
提交
取消