C# 泛型类属性

C# 泛型类属性,c#,generics,C#,Generics,我需要将类的属性设置为泛型。。我有一个json,有时对象属性可以以空值到达。 例如: 有时她会这样到达: { "id": 111047, "name": "TV", "active": true, "id_category": 318, "parent": { "id": 111046, "name": "LCD", "active": true,

我需要将类的属性设置为泛型。。我有一个json,有时对象属性可以以空值到达。 例如:

有时她会这样到达:

{
        "id": 111047,
        "name": "TV",
        "active": true,
        "id_category": 318,
        "parent": {
           "id": 111046,
           "name": "LCD",
           "active": true,
           "id_category": 317,
           "parent": null,
           "sellerId": null
       },
       "sellerId": 50
}
{
  "id": 111046,
  "name": "LCD",
  "active": true,
  "id_category": 317,
  "parent": null,
  "sellerId": 50
}
有时是这样的:

{
        "id": 111047,
        "name": "TV",
        "active": true,
        "id_category": 318,
        "parent": {
           "id": 111046,
           "name": "LCD",
           "active": true,
           "id_category": 317,
           "parent": null,
           "sellerId": null
       },
       "sellerId": 50
}
{
  "id": 111046,
  "name": "LCD",
  "active": true,
  "id_category": 317,
  "parent": null,
  "sellerId": 50
}
我试试这个:

public class CompleteCategory
{
        public string id { get; set; }
        public string name { get; set; }
        public string active { get; set; }
        public string id_category { get; set; }
        public CompleteCategory? parent { get; set; }
        public string sellerId { get; set; }
}
但他给了我这个错误:

错误1类型“CompleteCategory”必须是不可为空的值类型 以将其用作泛型类型或方法中的参数“T” “System.Nullable”


我想知道,我如何做到这一点???

只需在类型名称(
CompleteCategory
)后省略
)。它是一个
,因此它已经可以为空。
字符用于生成不可为null的类型nullabe,但是,正如错误消息所述,它不能用于已经可为null的类型


该类型完全不需要是泛型就可以执行您试图执行的操作。

删除可为空的符号“?”:
public CompleteCategory父级{get;set;}
CompleteCategory
是一个类(即a),因此它可以为空。 另一方面,值类型(如int或DateTime)不能为null(因此在本例中,可以使用
强制其“可为null”)


因此,只需从属性声明中删除

该错误告诉您CompleteCategory已经是类而不是值类型。这意味着CompleteCategory已经可以为null。大多数值类型不能为null,但也有例外。C语言具有可空值类型(具体来说,<>代码>可空< /COD>是可空值类型)。没错,但我认为可空的 >“处理”使值类型无效。你知道其他的例外情况吗?嗯,有一些指针。你如何对它们进行分类……很难。