简体   繁体   English

使用类型为String的Generic类并获取错误'string'必须是非抽象类型

[英]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 显然它是一种完全不同的方法调用

The new constraint new约束

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.

相关问题 通用 Class 加载程序:“T”必须是具有公共无参数构造函数的非抽象类型 - Generic Class Loader: 'T' must be a non-abstract type with a public parameterless constructor 在方法中使用非抽象类型作为泛型类型的问题 - Problem with using a non-abstract type as a generic type in a method 错误:“ T”必须是具有公共无参数构造函数的非抽象类型 - Error: 'T' must be a non-abstract type with a public parameterless constructor 获取“T”必须是具有公共无参数构造函数的非抽象类型,以便在泛型类型或方法中将其用作参数“T” - Getting 'T' 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 类映射错误:“ T”必须是具有公共无参数构造函数的非抽象类型 - Class Mapping Error: 'T' must be a non-abstract type with a public parameterless constructor T必须是具有公共无参数构造函数的非抽象类型,以便在泛型类型或方法中将其用作参数“TModel” - T must be a non-abstract type with a public parameterless constructor in order to use it as parameter 'TModel' in the generic type or method 'MvxWpfSetup<app> ' 必须是具有公共无参数构造函数的非抽象类型</app> - 'MvxWpfSetup<App>' must be a non-abstract type with a public parameterless constructor 必须是非抽象类型,并且在Redis中具有公共无参数构造函数 - must be a non-abstract type with a public parameterless constructor in redis 必须是具有公共无参数构造函数的非抽象类型 .net 核心 - must be a non-abstract type with a public parameterless constructor .net Core 错误:'T'必须是具有公共无参数构造函数的非抽象类型才能将其用作参数'T' - Error: 'T' must be a non-abstract type with a public parameterless constructor in order to use it as parameter 'T'
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM