如何使用传入的 Id 参数初始化 T 的新对象?
private ICollection<T> AddRelationalData<T>(List<int> relationalDataIds)
where T : class, new()
{
var relationalDataCollection = new Collection<T>()
if (relationalDataIds != null && relationalDataIds.Count > 0)
{
foreach (var entry in relationalDataIds.Select(id => new T {Id = id}))
{
relationalDataCollection.Add(entry);
}
}
return relationalDataCollection;
}
请您参考如下方法:
您应该使用包含所需属性的基类:
public class Test
{
public int Id {get;set;}
}
private ICollection<T> AddRelationalData<T>(List<int> relationalDataIds)
where T : Test, new()
{
var relationalDataCollection = new Collection<T>()
if (relationalDataIds != null && relationalDataIds.Count > 0)
{
foreach (var entry in relationalDataIds.Select(id => new T {Id = id}))
{
relationalDataCollection.Add(entry);
}
}
return relationalDataCollection;
}
