C#采用反射获取字段的名字和注释自动添加到表格显示和修改

为了方便数据显示和修改
采用反射获取字段的名字和注释
自动将字段添加到表格中
在表格中修改数据后可以直接保存到对象中

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using Newtonsoft.Json;
using System.Reflection;
using System.Runtime.Serialization.Formatters;

namespace json
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();

            string mtext = JsonConvert.SerializeObject(ObjDataA);
            richTextBox1.Text = mtext;


        }
        public static DataA ObjDataA = new DataA()
        {
            X = 1.1,
            Y = 2,
            S = "测试",
            mm = new DataB { X = 123 }
        
        };
        public static string GetEnumDescription<T>(T obj)
        {
            var type = obj.GetType();
            FieldInfo field = type.GetField(Enum.GetName(type, obj));
            DescriptionAttribute descAttr = Attribute.GetCustomAttribute(field, typeof(DescriptionAttribute)) as DescriptionAttribute;
            if (descAttr == null)
                return string.Empty;
            return descAttr.Description;
        }

        public static string GetEnumDescrip(FieldInfo field)
        {
        
            DescriptionAttribute descAttr = Attribute.GetCustomAttribute(field, typeof(DescriptionAttribute)) as DescriptionAttribute;
            if (descAttr == null)
                return string.Empty;
            return descAttr.Description;
        }
        public class DataA
        {
            [Description("方向X位置")]
            public double X;
            [Description("方向Y位置")]
            public int Y;
            [Description("说明信息")]
            public string S;
            [Description("特殊字符")]
            public DataB mm ;
        }
        
        public class DataB
        {
            public double X;
            public int Y;
        }

   
        private void button1_Click(object sender, EventArgs e)
        {
            var obj = Newtonsoft.Json.Linq.JObject.Parse(JsonConvert.SerializeObject(ObjDataA));
            richTextBox1.Text = obj.ToString();
            //string mtext = JsonConvert.SerializeObject(ObjDataA);
            //richTextBox1.Text = mtext;
        }

        private void button2_Click(object sender, EventArgs e)
        {

           
            var obj = Newtonsoft.Json.Linq.JObject.Parse(JsonConvert.SerializeObject(ObjDataA));
            Type t = typeof(DataA);
            FieldInfo[] fields = t.GetFields(BindingFlags.Public | BindingFlags.Instance | BindingFlags.Static);
            MyDge.Rows.Clear();
            int InsertClassId = MyDge.Rows.Add(new DataGridViewGroupCell(), new DataGridViewTextBoxCell(), new DataGridViewButtonCell());
            MyDge.Rows[InsertClassId].Cells[0].Value = t.Name;
            MyDge.Rows[InsertClassId].Cells[1].Value = "";
            MyDge.Rows[InsertClassId].Cells[2].Value = "";
            foreach (FieldInfo field in fields)
            {
                int InsertFieldId = MyDge.Rows.Add(new DataGridViewGroupCell(), new DataGridViewTextBoxCell(), new DataGridViewButtonCell());
                MyDge.Rows[InsertFieldId].Cells[0].Value = t.Name;
                MyDge.Rows[InsertFieldId].Cells[1].Value = field.Name+ GetEnumDescrip(field);
                MyDge.Rows[InsertFieldId].Cells[2].Value = obj[field.Name].ToString();
                ((DataGridViewGroupCell)MyDge.Rows[InsertClassId].Cells[0]).AddChildCell((DataGridViewGroupCell)MyDge.Rows[InsertFieldId].Cells[0]);
            }
            MyDge.Update();
        }

        private void button3_Click(object sender, EventArgs e)
        {
            string mtext = richTextBox1.Text;

            var obj = Newtonsoft.Json.Linq.JObject.Parse(JsonConvert.SerializeObject(ObjDataA));
            Type t = typeof(DataA);
            FieldInfo[] fields = t.GetFields(BindingFlags.Public | BindingFlags.Instance | BindingFlags.Static);
            for (int i = 0; i < MyDge.Rows.Count; i++)
            {
                var row = MyDge.Rows[i];
                foreach (FieldInfo field in fields)
                {
                    if (row.Cells[0].Value.ToString() == t.Name)
                    {
                        if (row.Cells[1].Value.ToString() == field.Name + GetEnumDescrip(field))
                        {

                                obj[field.Name] = row.Cells[2].Value.ToString();
                        }
                    }
                }
            }
            //为了处理叠加类DataB字段mm的异常
            string ss = obj.ToString();
            ss = ss.Replace("\\r\\n", "");
            ss = ss.Replace("\r\n", "");
            ss = ss.Replace("\\", "");
            ss = ss.Replace(": \"{", ":{");
            ss = ss.Replace("}\"", "}");

            ObjDataA = JsonConvert.DeserializeObject<DataA>(ss);
        }
    }


}

C#采用反射获取字段的名字和注释自动添加到表格显示和修改_第1张图片

你可能感兴趣的