public List<List<T>> BatchResultsList<T>(List<T> objectList)
{
foreach(T t in objectList)
{
// do something with T.
// note that since the type of T isn t constrained, the compiler can t
// tell what properties and methods it has, so you can t do much with it
// except add it to a collection or compare it to another object.
}
}
如果你需要限制T型号,以便你只处理特定种类的物体,使测试结果和结果链接能够使用一个接口,例如IResult。 然后:
public interface IResult
{
void DoSomething();
}
public class TestResult : IResult { ... }
public class ResultLinks : IResult { ... }
public List<List<T>> BatchResultsList<T>(List<T> objectList) where T : IResult
{
foreach(T t in objectList)
{
t.DoSomething();
// do something with T.
// note that since the type of T is constrained to types that implement
// IResult, you can access all properties and methods defined in IResult
// on the object t here
}
}
在你称之为这一方法时,你当然可以忽略这种参数,因为可以推断:
List<TestResult> objectList = new List<TestResult>();
List<List<TestResult>> list = BatchResultsList(objectList);