【问题标题】:Generate Avro schema from C# class从 C# 类生成 Avro 模式
【发布时间】:2021-09-21 13:26:19
【问题描述】:

我想知道是否有工具可以从 C# 类文件中获取信息并生成 Avro 模式。就像 Protobuf-net 一样,它提供了一些属性和一些工具,不需要用户手动实现 schema 文件。

[ProtoContract]
class Person {
[ProtoMember(1)]
public int Id {get;set;}
[ProtoMember(2)]
public string Name {get;set;}
[ProtoMember(3)]
public Address Address {get;set;}
}

【问题讨论】:

  • 能否请您展示一个 Avro 类的示例?不清楚您期望什么输出。 Avro 还需要读取器和写入器架构。

标签: c# .net avro


【解决方案1】:

我们使用 PropertyInfo 来获取所有的自定义属性

对不起,我无法粘贴所有代码:)

属性定义

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
[AttributeUsage(AttributeTargets.Property, AllowMultiple = false)]
public class EditAttribute : Attribute
{
    public EditType Type{ get; set; }
}

入口类

public class Admin
{
    [Edit(Type = EditType.Int, IsNumber = true, IsKey = true, Show = ShowType.All)]
    public int Id { get; set; }
}

获取属性的方法

System.Reflection.PropertyInfo[] properties = type.GetProperties();
foreach (PropertyInfo property in properties)
{
    EditAttribute editContent = (EditAttribute)property.GetCustomAttribute(typeof(EditAttribute));
}

【讨论】:

  • 这和 Avro 有什么关系?
猜你喜欢
  • 1970-01-01
  • 2019-09-24
  • 2014-08-24
  • 2013-03-24
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2019-11-22
  • 2020-11-26
相关资源
最近更新 更多