0

我有一个像这样的通用类:

public class Property<TObjectType>
{
}

我想使用新的System.Text.Json 源代码生成器,但它似乎不适用于泛型类。JsonSerializerContext这是该类的派生:

[JsonSerializable(typeof(Property<>))]
public partial class PropertyJsonContext<TObjectType> : JsonSerializerContext
{
}

该错误有点奇怪,因为它使所有其他非泛型JsonSerializerContext实现因以下 2 个错误而失败:

Error CS0534: 'XXXJsonContext' does not implement inherited abstract member 'JsonSerializerContext.GeneratedSerializerOptions.get'

Error CS0534: 'XXXJsonContext' does not implement inherited abstract member 'JsonSerializerContext.GetTypeInfo(Type)'

还有一个我觉得与我的问题有关的警告:

CSC : warning CS8785: Generator 'JsonSourceGenerator' failed to generate source. It will not contribute to the output and compilation errors may occur as a result. Exception was of type 'ArgumentException' with message 'The hintName 'PropertyJsonContext`1.NSPropertyTObjectType.g.cs' contains an invalid character '`' at position 21.

然后我更改了派生类以删除通用性:

[JsonSerializable(typeof(NSProperty<>))]
public partial class NSPropertyJsonContext : JsonSerializerContext
{
}

我得到这个错误:

  The type or namespace name 'TObjectType' could not be found in the global namespace (are you missing an assembly reference?)
4

0 回答 0