2012-04-20 97 views
1

我使用一個自定義的攔截行爲來篩選記錄(過濾器是基於誰是當前用戶),但是我有一些困難(這是攔截的身體Invoke方法)團結攔截 - 自定義攔截行爲

var companies = methodReturn.ReturnValue as IEnumerable<ICompanyId>; 
List<string> filter = CompaniesVisibleToUser(); 

methodReturn.ReturnValue = companies.Where(company =>  
    filter.Contains(company.CompanyId)).ToList(); 

的CompaniesVisibleToUser規定,允許用戶查看公司的ID的字符串列表。

我的問題是輸入的數據 - 公司 - 將不同類型的所有這一切應該實現ICompanyId對companyId要過濾數據的IList中。然而,看起來這個轉換 - 就像IEnumerable一樣,導致數據以這種類型返回,這導致了調用堆棧的進一步問題。

如何在不更改返回類型的情況下執行過濾器?

我得到的例外是

無法轉換類型的對象 'System.Collections.Generic.List 1[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>它能正常工作,但顯然這不是我想要的東西作爲被過濾列表將並不總是那種類型。

+0

你是什麼意思,演員'IEnumerable '是造成一些轉換問題?內存類型中的對象從不改變。 – Tejs 2012-04-20 18:01:01

+0

@David:什麼是原始返回類型? – 2012-04-20 18:52:52

+0

@Tejs我編輯了我的問題以包含更多詳細信息 – David 2012-04-20 19:11:13

回答

0

當你做對分配:

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; 
} 

這兩者應該工作。