1,使用反映将一个对象的同名属性赋值给另一个对象
2, DataTable 转换成一个实体
3,使用反射动态执行方法
4,根据属性信息来执行对应的方法
cs
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 = ToPageInfo<PB>(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);
List<PA> pAs = GetValue<PA>(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 ToPageInfo<T>(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 List<T> GetValue<T>(DataTable dt) where T : new()
{
List<T> list = new List<T>();
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");
}
}
}