English 中文(简体)
如何使通用名单与另一个通用名单相等
原标题:How to make a generic list equal another generic list
  • 时间:2010-02-03 04:30:26
  •  标签:
  • c#

这是我确定的。

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

这样做的最佳方式是什么?

问题回答

使用Cast<IPeriodigation>:

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

我认为,C#4是,如果你正在使用某种执行,IE amountable<out T>,你可以简单地这样做,并用,Covariance

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

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

Try return this.costPeriodList.Cast<IPeriodation>ToList()

从一个顺序到另一个顺序的LINQ方法不会是 平等<>。 也就是说,如果你使用<条码>Cast()/ToList(,那么以下测试就会失败。

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

此外,使用这些方法意味着,如果你试图在一次收集中增加一个项目,则不会反映在另一卷中。 每次你称之为“CalcList”时,都会建立一个全新的收集系统,视有多少件物品、多件物品,等等,这种收集可能会产生灾难性的后果。

我认为,更好的解决办法是:不使用<代码>List<T>持有成本PeriodDto,而是使用<代码>Collection<T>,并明确实施<编码>IEvidable<IPeriodation>。 如果需要,可选择执行<代码>IList<IPeriodigation>。

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




相关问题
Anyone feel like passing it forward?

I m the only developer in my company, and am getting along well as an autodidact, but I know I m missing out on the education one gets from working with and having code reviewed by more senior devs. ...

NSArray s, Primitive types and Boxing Oh My!

I m pretty new to the Objective-C world and I have a long history with .net/C# so naturally I m inclined to use my C# wits. Now here s the question: I feel really inclined to create some type of ...

C# Marshal / Pinvoke CBitmap?

I cannot figure out how to marshal a C++ CBitmap to a C# Bitmap or Image class. My import looks like this: [DllImport(@"test.dll", CharSet = CharSet.Unicode)] public static extern IntPtr ...

How to Use Ghostscript DLL to convert PDF to PDF/A

How to user GhostScript DLL to convert PDF to PDF/A. I know I kind of have to call the exported function of gsdll32.dll whose name is gsapi_init_with_args, but how do i pass the right arguments? BTW, ...

Linqy no matchy

Maybe it s something I m doing wrong. I m just learning Linq because I m bored. And so far so good. I made a little program and it basically just outputs all matches (foreach) into a label control. ...