C# 反射的使用及场景

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 = 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");
        }
    }
}

相关推荐

  1. c# 应用

    2024-03-29 12:58:06       14 阅读
  2. 理解

    2024-03-29 12:58:06       31 阅读
  3. 常见使用方式,反射基本教程

    2024-03-29 12:58:06       31 阅读
  4. Go中使用动态方法调用

    2024-03-29 12:58:06       33 阅读
  5. C/C++ 引用和指针区别使用场景

    2024-03-29 12:58:06       8 阅读

最近更新

  1. TCP协议是安全的吗?

    2024-03-29 12:58:06       18 阅读
  2. 阿里云服务器执行yum,一直下载docker-ce-stable失败

    2024-03-29 12:58:06       19 阅读
  3. 【Python教程】压缩PDF文件大小

    2024-03-29 12:58:06       18 阅读
  4. 通过文章id递归查询所有评论(xml)

    2024-03-29 12:58:06       20 阅读

热门阅读

  1. 深入理解与实战CSS变量

    2024-03-29 12:58:06       20 阅读
  2. Chapter 4 of Effective C++ (设计与声明)

    2024-03-29 12:58:06       18 阅读
  3. Shell教程_不同Shell中变量定义和使用的差异

    2024-03-29 12:58:06       17 阅读
  4. 掌握ChatGPT:轻松撰写学术论文的利器

    2024-03-29 12:58:06       18 阅读
  5. 如何拉取 GitHub 上项目的更新?

    2024-03-29 12:58:06       17 阅读
  6. nginx.conf相关

    2024-03-29 12:58:06       19 阅读
  7. vue 滚动条美化 css

    2024-03-29 12:58:06       17 阅读
  8. Linux系统使用服务实现开机自启动

    2024-03-29 12:58:06       18 阅读
  9. git ssh密钥配置 & 本地项目推送到github

    2024-03-29 12:58:06       18 阅读
  10. node整理学习(一)

    2024-03-29 12:58:06       16 阅读
  11. C++中vector的模拟实现

    2024-03-29 12:58:06       18 阅读