亚洲在线久爱草,狠狠天天香蕉网,天天搞日日干久草,伊人亚洲日本欧美

為了賬號安全,請及時綁定郵箱和手機立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

想要使Func <UnknownType,bool>其中UnknownType僅在運行時已知

想要使Func <UnknownType,bool>其中UnknownType僅在運行時已知

C#
素胚勾勒不出你 2021-04-29 14:10:45
因此,我正在嘗試使用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 回答

?
叮當貓咪

TA貢獻1776條經驗 獲得超12個贊

使用Object作為類型。那么您可以在運行時以及在將類型強制轉換為正確類型之后通過getType()檢查類型?;蚴褂脛討B以避免投射。


查看完整回答
反對 回復 2021-05-08
  • 2 回答
  • 0 關注
  • 145 瀏覽

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯系客服咨詢優惠詳情

幫助反饋 APP下載

慕課網APP
您的移動學習伙伴

公眾號

掃描二維碼
關注慕課網微信公眾號