这个样本:
public static void createDictionary<T>(IEnumerable<T> myRecords)
where T: T.ID // Wont Compile
{
IDictionary<int, T> dicionario = myRecords.ToDictionary(r => r.ID);
foreach (var item in dicionario)
{
Console.WriteLine("Key = {0}",item.Key);
Type thisType = item.Value.GetType();
StringBuilder sb = new StringBuilder();
foreach (var itemField in thisType.GetProperties())
{
sb.AppendLine(string.Format("{0} = {1}", itemField.Name, itemField.GetValue(item.Value, null)));
}
Console.WriteLine(sb);
}
}
如何强制作为参数传递的类型有一个名为“ID”的字段?