[英]Using Generic class with type String and getting error 'string' must be non-abstract type
I have a simple generic class as follows: 我有一个简单的泛型类如下:
public class DataResponse<T> where T : new()
{
public DataResponse()
{
this.Data = new List<T>();
IsSuccessful = true;
}
public bool IsSuccessful { get; set; }
public string[] ErrorMessages { get; set; }
public List<T> Data { get; set; }
}
It works just fine for every custom type I use, however in once instance I have a collection of data that is one field. 它适用于我使用的每个自定义类型,但是在一次实例中我有一个数据集合是一个字段。 Rather than make custom class with one field I would make the type string. 而不是使用一个字段制作自定义类,而不是制作类型字符串。 Doing so however returns the error: 但是这样做会返回错误:
var response = new DataResponse<String>();
'string' must be a non-abstract type with a public parameterless constructor in order to use it as parameter 'T' in the generic type or method 'DataResponse' 'string'必须是具有公共无参数构造函数的非抽象类型,才能在泛型类型或方法'DataResponse'中将其用作参数'T'
UPDATE I had added the where T : new() in response to encountering the problem originally. 更新我已经添加了T:new()来响应最初遇到问题。 Removing it solved it because it caused the IDE to highlight the correct line that was actually giving me the problem. 删除它解决了它,因为它导致IDE突出显示实际给我问题的正确行。 The line causing the error was to a method that does have the new() constraint. 导致错误的行是一个具有new()约束的方法。
Apparently its an entirely different call on a method 显然它是一种完全不同的方法调用
where T : new()
requires type T
to have public default constructor: 要求类型T
具有公共默认构造函数:
The new constraint specifies that any type argument in a generic class declaration must have a public parameterless constructor. 新约束指定泛型类声明中的任何类型参数都必须具有公共无参数构造函数。 To use the new constraint, the type cannot be abstract. 要使用新约束,类型不能是抽象的。
string
does not have one. string
没有。
Try to remove where T : new()
from 尝试删除where T : new()
的位置
public class DataResponse<T> where T : new()
This happens because String doesn't have public parameterless constructor which is required by : new() . 发生这种情况是因为String 没有以下所需的公共无参数构造函数: new() 。 You can confirm it here String Class 你可以在这里确认String Class
You should modify the code to this: 您应该将代码修改为:
public class DataResponse<T>
{
public DataResponse()
{
this.Data = new List<T>();
IsSuccessful = true;
}
public bool IsSuccessful { get; set; }
public string[] ErrorMessages { get; set; }
public List<T> Data { get; set; }
}
The new constraint specifies that any type argument in a generic class declaration must have a public parameterless constructor. 新约束指定泛型类声明中的任何类型参数都必须具有公共无参数构造函数。 To use the new constraint, the type cannot be abstract. 要使用新约束,类型不能是抽象的。
String does not have a parameterless constructor. String没有无参数构造函数。
http://msdn.microsoft.com/en-us/library/system.string(v=vs.110).aspx http://msdn.microsoft.com/en-us/library/system.string(v=vs.110).aspx
String doesn't have public parameter less constructor which is required for :new() and To use the new constraint, the type cannot be abstract. String没有公共参数less构造函数:new()和要使用新约束,类型不能是抽象的。
For your reference : String Class 供您参考:String Class
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.