2010-02-03 10 views
6

यह मेरा की स्थापना की है,कैसे बनाने के लिए एक सामान्य सूची एक और सामान्य सूची

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

क्या ऐसा करने का सबसे अच्छा तरीका होगा बराबर?

उत्तर

3

return this.costPeriodList.Cast<IPeriodCalculation>().ToList() आज़माएं।

9

उपयोग 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();   
     } 
    } 
} 

मैं अगर तुम IEnumerable<out T> को लागू करने के लिए कुछ इस्तेमाल कर रहे थे, सी # 4 में विश्वास करते हैं, तो आप इसे जिस तरह से आप यह लिखा था बस कर सकता है, और यह Covariance का उपयोग कर हल किया जा जाएगा।

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

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

LINQ तरीकों एक अनुक्रम से दूसरे में कास्ट करने के लिए बराबर नहीं होगा। यह कहना है कि यदि आप Cast()/ToList() का उपयोग करते हैं तो निम्न परीक्षण विफल हो जाएगा।

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

इसके अलावा, उन विधियों का उपयोग करने का अर्थ है कि यदि आपने एक संग्रह में कोई आइटम जोड़ने का प्रयास किया है, तो वे दूसरे में दिखाई नहीं देंगे। और हर बार जब आप कालकलिस्ट कहते हैं, तो यह एक पूरी तरह से नया संग्रह तैयार करेगा जो कितनी वस्तुओं, कितनी बार इसे बुलाया जाता है, इत्यादि के आधार पर विनाशकारी हो सकता है।

मेरी राय में एक बेहतर समाधान List<T> का उपयोग नहीं करना है CostPeriodDto को पकड़कर और इसके बजाय Collection<T> से प्राप्त संग्रह का उपयोग करें और स्पष्ट रूप से IEnumerable<IPeriodCalculation> लागू करें। वैकल्पिक रूप से आप IList<IPeriodCalculation> को लागू कर सकते हैं यदि आपको आवश्यकता हो।

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; } 
    } 
} 
संबंधित मुद्दे