"new" 的 C# 方法多重约束
C# method multiple constraints with "new"
我正在尝试定义一个具有多个约束的方法,其中 T
类型可以用作构造函数:
private void GetData<T, OType>(string url, string token1, string token2, Action<T, SqlConnection, SqlTransaction> bulkInsert, string user = null, string pwd = null)
where T : JsonElements<OType>, new
where OType : class
{
var thing = T();
我在 new
部分遇到错误,尽管说
) expected
正确的语法是什么?
您必须使用 new()
,而不仅仅是 new
。这是无参数构造函数的视觉线索。
此外,分配 thing
时必须使用 new T()
。
我正在尝试定义一个具有多个约束的方法,其中 T
类型可以用作构造函数:
private void GetData<T, OType>(string url, string token1, string token2, Action<T, SqlConnection, SqlTransaction> bulkInsert, string user = null, string pwd = null)
where T : JsonElements<OType>, new
where OType : class
{
var thing = T();
我在 new
部分遇到错误,尽管说
) expected
正确的语法是什么?
您必须使用 new()
,而不仅仅是 new
。这是无参数构造函数的视觉线索。
此外,分配 thing
时必须使用 new T()
。