首页 > 解决方案 > 从 C# 类生成 Avro 模式

问题描述

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

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

标签: c#.netavro

解决方案


我们使用 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));
}

推荐阅读