统一侦听-自定义侦听行为 [英] Unity Interception - Custom Interception Behaviour

查看:33
本文介绍了统一侦听-自定义侦听行为的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用自定义拦截行为来过滤记录(过滤器基于当前用户),但是我遇到了一些困难(这是拦截器调用方法的主体)

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.IList1[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;
}

这两种方法都应该有效。

这篇关于统一侦听-自定义侦听行为的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆