2010-02-03 12 views
6

To jest moja skonfigurować,Jak zrobić listę rodzajowy równa innej listy rodzajowy

class CostPeriodDto : IPeriodCalculation 
{ 
    public decimal? a { get; set; } 
    public decimal? b { get; set; } 
    public decimal? c { get; set; } 
    public decimal? d { get; set; } 
} 

interface IPeriodCalculation 
{ 
    decimal? a { get; set; } 
    decimal? b { get; set; } 
} 

class myDto 
{ 
    public List<CostPeriodDto> costPeriodList{ get; set; } 

    public List<IPeriodCalculation> periodCalcList 
    { 
     get 
     { 
      return this.costPeriodList; // compile error 
     } 
    } 
} 

Jaki byłby najlepszy sposób to zrobić?

Odpowiedz

3

Wypróbuj return this.costPeriodList.Cast<IPeriodCalculation>().ToList().

9

Zastosowanie Cast<IPeriodCalculation>():

public class CostPeriodDto : IPeriodCalculation 
{ 
    public decimal? a { get; set; } 
    public decimal? b { get; set; } 
    public decimal? c { get; set; } 
    public decimal? d { get; set; } 
} 

public interface IPeriodCalculation 
{ 
    decimal? a { get; set; } 
    decimal? b { get; set; } 
} 

public class myDto 
{ 
    public List<CostPeriodDto> costPeriodList { get; set; } 

    public List<IPeriodCalculation> periodCalcList 
    { 
     get 
     { 
      return this.costPeriodList.Cast<IPeriodCalculation>().ToList();   
     } 
    } 
} 

wierzę w C# 4, jeśli uzywasz coś wykonawczych IEnumerable<out T>, można po prostu zrobić to tak, jak to napisał, a to być rozwiązane za pomocą Covariance.

class myDto 
{ 
    public IEnumerable<CostPeriodDto> costPeriodList{ get; set; } 

    public IEnumerable<IPeriodCalculation> periodCalcList 
    { 
     get 
     { 
      return this.costPeriodList; // wont give a compilation error  
     } 
    } 
} 
1

Metody LINQ przesyłać z jednej sekwencji do drugiego nie będzie równa. Oznacza to, że poniższy test nie powiedzie się, jeśli użyjesz Cast()/ToList().

Assert.AreSame(myDto.costPeriodList, myDto.periodCalcList); 

Ponadto, stosując te same metody oznacza, że ​​jeśli próbował dodać element do jednej kolekcji, to nie być widoczne w drugiej. I za każdym razem, gdy wywoływano periodCalcList, tworzyłoby się całkowicie nową kolekcję, która mogłaby być katastrofalna w zależności od tego, ile elementów, jak często jest wywoływana, itp.

Lepszym rozwiązaniem, moim zdaniem, jest nieużywanie List<T> dla trzymając CostPeriodDto i zamiast tego użyj kolekcji pochodzącej z Collection<T> i jawnie implementuj IEnumerable<IPeriodCalculation>. Opcjonalnie możesz wprowadzić IList<IPeriodCalculation>, jeśli potrzebujesz.

class CostPeriodDtoCollection : 
    Collection<CostPeriodDto>, 
    IEnumerable<IPeriodCalculation> 
{ 

    IEnumerable<IPeriodCalculation>.GetEnumerator() { 
     foreach (IPeriodCalculation item in this) { 
      yield return item; 
     } 
    } 

} 

class MyDto { 
    public CostPeriodDtoCollection CostPeriods { get; set; } 
    public IEnumerable<IPeriodCalculation> PeriodCalcList { 
     get { return CostPeriods; } 
    } 
} 
Powiązane problemy