0

我想知道为什么我写下面的语句会出错,尽管我在课堂上提到了什么是 T

IList<T> targetObjectsCollection = new List<T>();
for (int counter = 0;  counter < dataTransferObjects.Count; counter++)
{
    targetObjectsCollection.Add(MappSharePointDAOToDTO(sharePointDaos[counter], dataTransferObjects[counter]));
}

当我将其更改为以下语句时,错误消失了??

IList<IMapperMarker> targetObjectsCollection = new List<IMapperMarker>();
for (int counter = 0;  counter < dataTransferObjects.Count; counter++)
{
    targetObjectsCollection.Add(MappSharePointDAOToDTO(sharePointDaos[counter], dataTransferObjects[counter]));
}

任何机构都可以描述。

4

1 回答 1

3

你好像没有定义T。这是一个占位符。它需要定义。

如果在T有定义的上下文中使用此代码,它可能会起作用。例如,

private IList<T> AddDataTransferObjects(IList<T> dataTransferObjects)
    : where T : IMapperMarker
{
    IList<T> targetObjectsCollection = new List<T>();
    for (int counter = 0;  counter < dataTransferObjects.Count; counter++)
    {
        targetObjectsCollection.Add(MappSharePointDAOToDTO(sharePointDaos[counter], dataTransferObjects[counter]));
    }
    return targetObjectsCollection;
}

如果你这样称呼它,如下所示:

IList<IMapperMarker> dtoList = Something();
var list = AddDataTransferObjects(dtoList);

在这种情况下,内部T将绑定到 type IMapperMarker

于 2013-05-30T04:44:57.250 回答