建设网站制作项目描述,简道云crm管理系统,利用表单大师做网站,wordpress 用户组权限设置1#xff0c;使用反映将一个对象的同名属性赋值给另一个对象
2, DataTable 转换成一个实体
3#xff0c;使用反射动态执行方法
4,根据属性信息来执行对应的方法
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using…1使用反映将一个对象的同名属性赋值给另一个对象
2, DataTable 转换成一个实体
3使用反射动态执行方法
4,根据属性信息来执行对应的方法
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Reflection;
using System.Windows.Forms;namespace WindowsFormsApp2
{public partial class FrmReflection : Form{public FrmReflection(){InitializeComponent();}private void FrmReflection_Load(object sender, EventArgs e){//反射的使用场景//1使用反映将一个对象的同名属性赋值给另一个对象PA pA new PA();pA.ID 3;pA.Name PAName1;pA.Remark PARemark;PB pB ToPageInfoPB(pA);MessageBox.Show(pB.Name);//2,DataTable 转换成一个实体DataTable dt new DataTable();dt.Columns.Add(ID, typeof(int) );dt.Columns.Add(Name, typeof(string));dt.Columns.Add(Remark, typeof(string));DataRow dr dt.NewRow();dr[ID] 1;dr[Name] PaName2;dt.Rows.Add(dr);ListPA pAs GetValuePA(dt);MessageBox.Show(pAs[0].Name);//3使用反射动态执行方法Assembly assembly Assembly.GetExecutingAssembly();Type[] t assembly.GetTypes(); //通过Assembly获取程序集中所有的类//Assembly assembly1 Assembly.LoadFrom(QRCoder.dll);//通过DLL文件全名反射其中的所有类型Type[] t1 assembly.GetTypes(); //通过Assembly获取程序指定dll集中所有的类/Assembly ass Assembly.Load(WindowsFormsApp2);//通过程序集的名称反射Type t2 ass.GetType(WindowsFormsApp2.FrmReflection);//命名空间类object o Activator.CreateInstance(t2, null, null);//实例一个对象MethodInfo mi t2.GetMethod(Show1);//获取方法mi.Invoke(o, null);//执行方法//4,根据属性信息来执行对应的方法Type t3 ass.GetType(WindowsFormsApp2.IsPrint);//命名空间类var attribute Attribute.GetCustomAttribute(t3, typeof(BarcodeTagAttrib)) as BarcodeTagAttrib;if (attribute.BarcodeTpName IsPrint){//var instance Activator.CreateInstance(t2) as CtrlPlanITag;MethodInfo mi3 t3.GetMethod(show2);//获取方法//instance.show2();//执行方法object o1 Activator.CreateInstance(t3, null, null);//实例一个对象mi3.Invoke(o1, null);//执行方法}}public T ToPageInfoT(object model) where T : new(){T t new T();PropertyInfo property null;foreach (var item in typeof(T).GetProperties()){property model.GetType().GetProperty(item.Name);if (property ! null){item.SetValue(t, property.GetValue(model, null), null);}}return t;}public ListT GetValueT(DataTable dt) where T : new(){ListT list new ListT();Type type typeof(T);for (int i 0; i dt.Rows.Count; i){T t new T();for (int j 0; j dt.Columns.Count; j){PropertyInfo pro type.GetProperty(dt.Columns[j].ColumnName);if (pro ! null){if (dt.Rows[i][j] ! DBNull.Value){pro.SetValue(t, dt.Rows[i][j], null);}}}list.Add(t);}return list;}public void Show1() {MessageBox.Show(Show1);}}public class PA{public int ID { get; set; }public string Name { get; set; }public string Description { get; set; }public string Remark { get; set; }}public class PB{public int ID { get; set; }public string Name { get; set; }public string DeviceType { get; set; }public string DeviceName { get; set; }public string Remark { get; set; }}public class BarcodeTagAttrib : Attribute{public string BarcodeTpName { get; set; }}public interface CtrlPlanITag{void show2();}[BarcodeTagAttrib(BarcodeTpName IsNeedEnd)]public class IsNeedEnd : CtrlPlanITag{public void show2(){MessageBox.Show(IsNeedEndShow);}}[BarcodeTagAttrib(BarcodeTpName IsPrint)]public class IsPrint : CtrlPlanITag{public void show2(){MessageBox.Show(IsPrintShow);}}
}