Unity Interception - Custom Interception Behaviour(统一侦听-自定义侦听行为)
问题描述
我正在使用自定义拦截行为来过滤记录(过滤器基于当前用户),但是我遇到了一些困难(这是拦截器调用方法的主体)
var companies = methodReturn.ReturnValue as IEnumerable<ICompanyId>;
List<string> filter = CompaniesVisibleToUser();
methodReturn.ReturnValue = companies.Where(company =>
filter.Contains(company.CompanyId)).ToList();
CompaniesVisibleToUser提供允许用户查看的公司ID的字符串列表。
我的问题是,传入的数据-公司-将是一个各种类型的IList,所有这些类型都应该实现ICompanyId,以便在pananyID上过滤数据。但是,强制转换为IEnumerable似乎会导致将数据作为此类型返回,这会在调用堆栈中进一步引发问题。
如何在不更改返回类型的情况下执行筛选?
我得到的异常是
无法强制转换类型为‘System.Collections.Generic.List1[PTSM.Application.Dtos.ICompanyId]' to type 'System.Collections.Generic.IList
1[PTSM.Application.Dtos.EmployeeOverviewDto]’.的对象
呼叫者越高
public IList<ApplicationLayerDtos.EmployeeOverviewDto> GetEmployeesOverview() { return _appraisalService.GetEmployeesOverview(); }
如果我更改
IEnumerable<ICompanyId>
到IEnumerable<EmployeeOverviewDto>
按预期工作,但显然这不是我想要的类型,因为要筛选的列表不会始终属于该类型。
推荐答案
当您进行作业时:
methodReturn.ReturnValue = companies.Where(company =>
filter.Contains(company.CompanyId)).ToList();
您正在将返回值设置为List<ICompanyId>
类型。
您可以将更高的调用函数更改为:
public IList<ApplicationLayerDtos.ICompanyId> GetEmployeesOverview()
{
return _appraisalService.GetEmployeesOverview();
}
或者您可以将其更改为以下内容:
public IList<ApplicationLayerDtos.EmployeeOverviewDto> GetEmployeesOverview()
{
var result = (List<EmployeeOverviewDto>)_appraisalService.GetEmployeesOverview().Where(x => x.GetType() == typeof(EmployeeOverviewDto)).ToList();
return result;
}
这两种方法都应该有效。
这篇关于统一侦听-自定义侦听行为的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:统一侦听-自定义侦听行为


- C#MongoDB使用Builders查找派生对象 2022-09-04
- 在哪里可以找到使用中的C#/XML文档注释的好例子? 2022-01-01
- WebMatrix WebSecurity PasswordSalt 2022-01-01
- 输入按键事件处理程序 2022-01-01
- Web Api 中的 Swagger .netcore 3.1,使用 swagger UI 设置日期时间格式 2022-01-01
- 良好实践:如何重用 .csproj 和 .sln 文件来为 CI 创建 2022-01-01
- MoreLinq maxBy vs LINQ max + where 2022-01-01
- C# 中多线程网络服务器的模式 2022-01-01
- 带有服务/守护程序应用程序的 Microsoft Graph CSharp SDK 和 OneDrive for Business - 配额方面返回 null 2022-01-01
- 如何用自己压缩一个 IEnumerable 2022-01-01