c# - 从 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;}
}
解决方案
我们使用 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));
}
推荐阅读
- javascript - 如何使用 Node.js 显示来自 MongoDB 的图像?
- java - 如何读取File到HashMap,更新HashMap,再更新File
- java - PostMagging 与 spring boot java 到 mysql
- azure - 在天蓝色搜索中索引动态字段
- c++ - (C++) 根据用户输入输入特定模式
- sql - Sql查询在出现时跳过集合中的字母
- php - 如何显示不同名称的用户
- php - 如何将多个变量传递给模态
- javascript - 如何在 javascript .scroll() 之后调用函数
- opencv - 当 CMatrix 转换为 slice 时,Go-vet 抱怨 unsafeptr