1

我正在尝试将新的 NET6 JSON 源代码生成与最少的 API 相结合,以尝试最大化性能。我有一个我试图与 Source Generation 一起使用的模型,它必须包含来自该库的引用属性:Microsoft.EntityFrameworkCore.SqlServer.NetTopologySuite

我从库中收到此错误:

Error   CS0122  'Geometry.SortIndexValue' is inaccessible due to its protection level

我想忽略整个属性,而不是将其包含在源代码生成中。那可能吗?

我的最小 API 代码如下所示: https ://github.com/aherrick/Net6JsonSourceGenerator

using Microsoft.AspNetCore.Mvc;
using NetTopologySuite.Geometries;
using System.Text.Json.Serialization;

var builder = WebApplication.CreateBuilder(args);

// Add services to the container.

var app = builder.Build();

builder.Services.Configure<JsonOptions>(options =>
{
    options.JsonSerializerOptions.AddContext<VenueJsonContext>();
});

app.MapGet("GetVenues", () =>
{
    var venues = new List<Venue>();

    return venues;
});

app.Run();

public class Venue
{
    public string Name { get; set; }

    [JsonIgnore] // don't even include this property in source generation ideally.
    public Geometry Location { get; set; }
}

[JsonSerializable(typeof(Venue[]))]
public partial class VenueJsonContext : JsonSerializerContext
{
}
4

0 回答 0