[英]c# models to protobuf messages (gRPC project)
我可以找到很多關於如何從 protobuf 消息生成 C# 模型的資源(它甚至內置在 Grpc.AspNetCore 包中),但反之則不行。
我創建了一個類似於此的 blazor webassembly 客戶端/服務器項目: https://github.com/stevejgordon/gRPCBasicSample/
我有一個域層,其中包含許多用於應用程序的 C# 模型。 我想將這些模型轉換為消息回復“查看模型”,而不是全部寫在手中(我什至不確定 c# 和 protobuf 之間的正確轉換)
示例:C# Model:
/// <summary>
/// The area.
/// </summary>
public class Area
{
/// <summary>
/// Initializes a new instance of the <see cref="Area"/> class.
/// </summary>
/// <param name="name">Name of the region.</param>
/// <param name="areaType">What type of area is the region.</param>
/// <param name="numberOfSupplies">number of supplies the region has.</param>
/// <param name="numberOfMusterCrows">number of crowns the region has.</param>
public Area(string name, AreaType areaType, int numberOfSupplies, int numberOfMusterCrows)
{
this.Name = name;
this.AreaType = areaType;
this.NumberOfSupplies = numberOfSupplies;
this.NumberOfMusterCrowns = numberOfMusterCrows;
this.CurrentArmy = new Collection<Unit>();
this.AdjacentAreas = new Collection<Area>();
}
/// <summary>
/// Gets the name of the region.
/// </summary>
public string Name { get; }
/// <summary>
/// Gets or sets which house the area is controlled by.
/// </summary>
public House ControlledBy { get; set; }
/// <summary>
/// Gets the type of area the region is.
/// </summary>
public AreaType AreaType { get; }
/// <summary>
/// Gets the adjacent regions the region has.
/// </summary>
public IEnumerable<Area> AdjacentAreas { get; internal set; }
/// <summary>
/// Gets the number of supplies (barrels) the region has.
/// </summary>
public int NumberOfSupplies { get; }
/// <summary>
/// Gets the number of crowns the region has.
/// </summary>
public int NumberOfMusterCrowns { get; }
/// <summary>
/// Gets or sets a value indicating whether the region is occupied by troops.
/// </summary>
public bool IsOccupied { get; set; }
/// <summary>
/// Gets a value indicating whether the region has a stronghold.
/// </summary>
public bool HasStronghold { get; }
/// <summary>
/// Gets a value indicating whether the region has a castle.
/// </summary>
public bool HasCastle { get; }
/// <summary>
/// Gets or sets the current armies occupying the region.
/// </summary>
public ICollection<Unit> CurrentArmy { get; set; }
/// <summary>
/// Gets or sets the current order token placed.
/// </summary>
public OrderToken? PlacedOrderToken { get; set; }
}
應該變成這樣的東西(簡化):
message AreaReply {
string name = 1;
enum AreaType {
Land = 0;
Water = 1;
}
AreaType areaType = 4;
House controlledBy = 5;
}
message House {
google.protobuf.StringValue name = 1;
}
真的沒有這樣的發電機嗎? :)
謝謝
在 class 定義之上使用此注釋:
[ProtoContract(ImplicitFields = ImplicitFields.AllPublic)]
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.