public static void Main2() { #region 演示一:动态生成类。 //生成一个类t。 Type t = ClassDynamicCreater.BuildType("MyClass"); #endregion #region 演示二:动态添加属性到类。 //先定义两个属性。 List <CustPropertyInfo> lcpi = new List <CustPropertyInfo>(); CustPropertyInfo cpi; cpi = new CustPropertyInfo("System.String", "S1"); lcpi.Add(cpi); cpi = new CustPropertyInfo("System.String", "S2"); lcpi.Add(cpi); //再加入上面定义的两个属性到我们生成的类t。 t = ClassDynamicCreater.AddProperty(t, lcpi); //把它显示出来。 DispProperty(t); //再定义三个属性。 lcpi.Clear(); cpi = new CustPropertyInfo(SystemDataType.Int, "I1"); //"System.Int32" lcpi.Add(cpi); cpi = new CustPropertyInfo("System.Int32", "I2"); lcpi.Add(cpi); cpi = new CustPropertyInfo("System.Object", "O1"); lcpi.Add(cpi); //再加入上面定义的三个属性到我们生成的类t。 t = ClassDynamicCreater.AddProperty(t, lcpi); //再把它显示出来,看看有没有增加到5个属性。 DispProperty(t); #endregion #region 演示三:动态从类里删除属性。 //把'S1'属性从类t中删除。 t = ClassDynamicCreater.DeleteProperty(t, "S1"); //显示出来,可以看到'S1'不见了。 DispProperty(t); #endregion #region 演示四:动态获取和设置属性值。 //先生成一个类t的实例o。 object o = ClassDynamicCreater.CreateInstance(t); //给S2,I2,O1属性赋值。 ClassDynamicCreater.SetPropertyValue(o, "S2", "abcd"); ClassDynamicCreater.SetPropertyValue(o, "I2", 1234); ClassDynamicCreater.SetPropertyValue(o, "O1", 7.5); //再把S2,I2,01的属性值显示出来。 Console.WriteLine("S2 = {0}", ClassDynamicCreater.GetPropertyValue(o, "S2")); Console.WriteLine("I2 = {0}", ClassDynamicCreater.GetPropertyValue(o, "I2")); Console.WriteLine("O1 = {0}", ClassDynamicCreater.GetPropertyValue(o, "O1")); #endregion #region 演示五:类嵌套设置属性值 Type t2 = ClassDynamicCreater.BuildType("MyClass2"); List <CustPropertyInfo> lcpi2 = new List <CustPropertyInfo>(); CustPropertyInfo cpi2; //cpi2 = new CustPropertyInfo(t.FullName, "mc"); //t2类中加入一个t属性 cpi2 = new CustPropertyInfo(t, "MC"); //t2类中加入一个t属性 lcpi2.Add(cpi2); TypeInfo tInfo = t.GetTypeInfo(); //t2类中加入一个t类的列表属性 //cpi2 = new CustPropertyInfo(typeof(List<>), "MCL"); cpi2 = new CustPropertyInfo(typeof(List <>), t, "MCL"); lcpi2.Add(cpi2); t2 = ClassDynamicCreater.AddProperty(t2, lcpi2); DispProperty(t2); //先生成一个类t2的实例o。 object o2 = ClassDynamicCreater.CreateInstance(t2); //给MC属性赋值。 ClassDynamicCreater.SetPropertyValue(o2, "MC", o); //o是类t的一个实例 //给MCL属性赋值。 dynamic Dynamic = ClassDynamicCreater.CreateGeneric(typeof(List <>), t); //object o = ClassDynamicCreater.CreateInstance(t); dynamic o1 = o; Dynamic.Add(o1); ClassDynamicCreater.SetPropertyValue(o2, "MCL", Dynamic); //再把mc属性值显示出来。 Console.WriteLine("MC = {0}", ClassDynamicCreater.GetPropertyValue(o2, "MC")); #endregion object genericList = ClassDynamicCreater.CreateGeneric(typeof(List <>), typeof(StudentTest)); dynamic dynamic = genericList; dynamic.Add(new StudentTest()); Console.Read(); }
public Control CreateSettingControl(CustClassInfo item) { XtraUserControl xtraUserControl = new XtraUserControl(); xtraUserControl.AutoScroll = true; xtraUserControl.Dock = DockStyle.Fill; xtraUserControl.Text = item.ClassName; SimpleButton button = new SimpleButton() { Text = "导出报表" }; button.Appearance.Font = new System.Drawing.Font("微软雅黑", 15F, System.Drawing.FontStyle.Bold, System.Drawing.GraphicsUnit.Point, ((byte)(0))); button.Appearance.Options.UseFont = true; button.Dock = System.Windows.Forms.DockStyle.Bottom; button.Location = new System.Drawing.Point(0, 0); button.Size = new System.Drawing.Size(240, 50); xtraUserControl.Controls.Add(button); ProgressBarControl progressBar = new ProgressBarControl(); progressBar.Dock = DockStyle.Bottom; progressBar.Properties.Appearance.BackColor = System.Drawing.Color.Gainsboro; progressBar.Properties.EndColor = System.Drawing.Color.FromArgb(((int)(((byte)(192)))), ((int)(((byte)(255)))), ((int)(((byte)(255))))); progressBar.Properties.LookAndFeel.Style = DevExpress.LookAndFeel.LookAndFeelStyle.Flat; progressBar.Properties.LookAndFeel.UseDefaultLookAndFeel = false; progressBar.Properties.StartColor = System.Drawing.Color.FromArgb(((int)(((byte)(192)))), ((int)(((byte)(255)))), ((int)(((byte)(192))))); progressBar.Size = new System.Drawing.Size(240, 30); xtraUserControl.Controls.Add(progressBar); //XtraForm form = new XtraForm(); if (item.ListOrSingle) { GridView gv = new GridView(); gv.OptionsView.ShowGroupPanel = false; gv.OptionsView.NewItemRowPosition = NewItemRowPosition.Top; gv.OptionsView.ShowFooter = true; GridControl gc = new GridControl(); gc.Dock = DockStyle.Fill; gc.MainView = gv; gc.ViewCollection.AddRange(new DevExpress.XtraGrid.Views.Base.BaseView[] { gv }); gv.GridControl = gc; if (item.ClassType == null) //如果还嵌套List { GridView gvSub = new GridView(); //最多嵌套两层 gvSub.OptionsView.ShowGroupPanel = false; gvSub.OptionsView.NewItemRowPosition = NewItemRowPosition.Top; gvSub.OptionsView.ShowFooter = true; gvSub.GridControl = gc; } object genericList = ClassDynamicCreater.CreateGeneric(typeof(BindingList <>), item.ClassType); if (item.DataSourceTag == null || item.DataSourceTag.GetType() != genericList.GetType()) { item.DataSourceTag = genericList; } //IBindingList ibl = item.DataSourceTag as IBindingList; gc.DataSource = item.DataSourceTag; //form.Controls.Add(gc); xtraUserControl.Controls.Add(gc); button.Click += (o, e) => { if (!Directory.Exists(ExportDirectory)) { Directory.CreateDirectory(ExportDirectory); } progressBar.Position = 0; gv.ExportToXlsx($@"{ExportDirectory}{item.ClassName}.xlsx"); progressBar.Position = 100; Task.Run(() => { Process.Start("explorer.exe", ExportDirectory); }); }; } else { PropertyGridControl pc = new PropertyGridControl(); pc.Dock = DockStyle.Fill; pc.SelectedObject = item.Instance; //form.Controls.Add(pc); xtraUserControl.Controls.Add(pc); button.Click += (o, e) => { if (!Directory.Exists(ExportDirectory)) { Directory.CreateDirectory(ExportDirectory); } progressBar.Position = 0; pc.ExportToXlsx($@"{ExportDirectory}{item.ClassName}.xlsx"); progressBar.Position = 100; Task.Run(() => { Process.Start("explorer.exe", ExportDirectory); }); }; } return(xtraUserControl); }
private void 查看_ButtonClick(object sender, DevExpress.XtraEditors.Controls.ButtonPressedEventArgs e) { int i = this.gridView1.FocusedRowHandle; if (i >= 0) { CustClassInfo item = CustClassManager.Instance.AllClasses[i]; XtraForm form = new XtraForm(); if (item.ListOrSingle) { GridView gv = new GridView(); gv.OptionsView.ShowGroupPanel = false; gv.OptionsView.NewItemRowPosition = NewItemRowPosition.Top; gv.OptionsView.ShowFooter = true; GridControl gc = new GridControl(); gc.Dock = DockStyle.Fill; gc.MainView = gv; gc.ViewCollection.AddRange(new DevExpress.XtraGrid.Views.Base.BaseView[] { gv }); gv.GridControl = gc; foreach (var itemP in item.ClassType.GetProperties()) { Type typeP = itemP.PropertyType; if (typeP.IsGenericType) //判断是否有属性为List<> //注意这里最多嵌套两层,三层或以上的情况懒得再递归了,一般最多两层 { Type listSubType = typeP.GetGenericArguments()[0]; //如List<string>获取到string type GridView gvSub = new GridView(); gvSub.OptionsView.ShowGroupPanel = false; gvSub.OptionsView.NewItemRowPosition = NewItemRowPosition.Top; gvSub.OptionsView.ShowFooter = true; gvSub.GridControl = gc; GridLevelNode gridLevelNode = new GridLevelNode(); gridLevelNode.LevelTemplate = gvSub; gridLevelNode.RelationName = $@"{itemP.Name}"; gc.LevelTree.Nodes.AddRange(new GridLevelNode[] { gridLevelNode }); } } object genericList = ClassDynamicCreater.CreateGeneric(typeof(BindingList <>), item.ClassType); if (item.DataSourceTag == null || item.DataSourceTag.GetType() != genericList.GetType()) { item.DataSourceTag = genericList; } //IBindingList ibl = item.DataSourceTag as IBindingList; gc.DataSource = item.DataSourceTag; form.Controls.Add(gc); } else { PropertyGridControl pc = new PropertyGridControl(); pc.Dock = DockStyle.Fill; pc.SelectedObject = item.Instance; form.Controls.Add(pc); } form.ShowDialog(); } }