C# 错误:';T';必须是具有公共无参数构造函数的非抽象类型,才能将其用作参数';T';

C# 错误:';T';必须是具有公共无参数构造函数的非抽象类型,才能将其用作参数';T';,c#,generics,C#,Generics,我试图实现一个方法,它接受一个泛型T并返回一个T列表 public static List<T> GetMessages<T>(string query, int count, SqlConnection sqlconnection) where T : ITable, new() { List<T> messages = new List<T>(); List<T> er

我试图实现一个方法,它接受一个泛型T并返回一个T列表

    public static List<T> GetMessages<T>(string query, int count, SqlConnection sqlconnection)
        where T : ITable, new()
    {
        List<T> messages = new List<T>();
        List<T> errorMessages = new List<T>();

        DataSet response = DBUtils.ExecuteQueryScriptWithConnection(query, sqlconnection);

        for (int i = 0; i < response.Tables[0].Rows.Count; ++i)
        {
            T message = new T();

            DataRow row = response.Tables[0].Rows[i];
            message.Id = Convert.ToInt32(row[0]);
            message.CreatedDate = Convert.ToDateTime(row[1]);                
        }

        return messages;
    }

我做错了什么?

调用代码中的
T
需要具有相同的约束


否则,有人可以用<代码> T 调用你的包装函数,它没有构造函数。在这种情况下,你应该考虑这个答案。@ Switk,如你的链接所解释的,提问者仍然需要等待6分钟才能接受答案。请考虑给他一些松弛。

      List<T> messages = TableHelpers.GetMessages<T>(query, 1000, sqlconnection);
public class MessageReceived : ITable
{
    public int Id { get; set; }
    public DateTime CreatedDate { get; set; }

    string query = String.Format(@"select Id, CreatedDate, from MessageReceived where createddate > '2013-04-18 00:00:00.0000000'");

    public MessageReceived()
    {

    }
}