源生成器不自动生成代码, 编译也报错.
检查半天, 发现生成的jsoncontext放在嵌套类里, 而其母类没有加partial
public partial class jsonio { //!!!! 注意母类也要partial !!!!
public class benchinference_data {
[JsonPropertyName("signals")]
public float[,,] signals { get; set; }
[JsonPropertyName("outfeatures")]
public float[,] outfeatures { get; set; }
[JsonPropertyName("outclassified")]
public float[,] outclassified { get; set; }
}
[JsonSourceGenerationOptions(WriteIndented =true,GenerationMode =JsonSourceGenerationMode.Serialization)]
[JsonSerializable(typeof(benchinference_data))]
public partial class benchinference_data_jsoncontext : JsonSerializerContext { }
}
另列举需要检查的几点方便兄弟们:
- 源码配置, 见贴的源码
- 确保nuget引用了system.text.json, 这个一般自动处理, 不过最好检查下
- csproj中配置EnableSourceGenerator, 见下面贴的源码
- 如果jsoncontext被生成类是嵌套类, 则母类也要partial
<!--源生成和源生成调试-->
<EmitCompilerGeneratedFiles>true</EmitCompilerGeneratedFiles>
<EnableSourceGenerator>true</EnableSourceGenerator>
1597

被折叠的 条评论
为什么被折叠?



