不知道哪上传。。 user.cs using System; using System.Collections.Generic; using System.Text; using System.Data; namespace MyTest1.Setup.Model { [Table("Ta_User", "用户表")] public class User { public User() { } [Col("序号", SqlDbType.Int, pk = true)] public int id; [Col("电子邮件", SqlDbType.NVarChar, 100)] public string email; [Col("密码", SqlDbType.NVarChar, 50)] public string pwd; [Col("创建日期", SqlDbType.NVarChar, 50)] public DateTime createdate; [Col("标记 -1删除 0禁用 1正常", SqlDbType.SmallInt)] public int flag; } } CustAttr.cs using System; using System.Reflection; using System.Data; namespace MyTest1 { /// <summary> /// 表 /// </summary> [AttributeUsage(AttributeTargets.Class, AllowMultiple = false)] public class TableAttribute : Attribute { /// <summary> /// 表描述 /// </summary> public string desc { get; set; } /// <summary> /// 表名 /// </summary> public string name { get; set; } public TableAttribute(string name, string desc) { this.name = name; this.desc = desc; } } /// <summary> /// 列 /// </summary> [AttributeUsage(AttributeTargets.All, AllowMultiple = false)] public class ColAttribute : Attribute { /// <summary> /// 描述 /// </summary> public string desc { get; set; } /// <summary> /// 主键 /// </summary> public bool pk { get; set; } /// <summary> /// 长度 /// </summary> public int size { get; set; } /// <summary> /// 类型 /// </summary> public SqlDbType type { get; set; } public ColAttribute(string desc, SqlDbType type) { this.desc = desc; this.type = type; } public ColAttribute(string desc, SqlDbType type, int size) { this.desc = desc; this.type = type; this.size = size; } } } app.cs using System; using System.Collections.Generic; using System.Text; namespace MyTest1 { class Program { static void Main(string[] args) { var myType = typeof(Setup.Model.User); var ss = myType.GetFields(); var dt = (TableAttribute)myType.GetCustomAttributes(typeof(TableAttribute), false)[0]; Console.WriteLine(dt.name + "(" + dt.desc + ")");//表名称和备注 foreach (var s in ss) { var a = (ColAttribute)s.GetCustomAttributes(false)[0]; var html = "{0}:{1}({2}) [{3}]";//字段名:类型(长度) [备注] Console.WriteLine(string.Format(html, s.Name, a.type.ToString().ToLower(), a.size, a.desc)); } Console.ReadKey(true); } } }