C#反射学习

定义:反射就是动态获取程序集中的元数据和类型并创建对象调用成员

要点:先获取指定类的Type对象,然后再通过Type类对象获取类的属性,方法,字段,事件,接口等

1、关键类:Type

                Type类可以获取类中的所有信息包括方法、属性等。可以动态调用类的属性方法,Type是对类的描述,主要针对类

 2、如何获取一个类的Type对象:

        1)如果该类有实例化的对象,则通过对象.GetType方法获取

        2)如过该类没有创建对象,则通过typeof(类名)获取      

3、通过Type可以获取当前类型的父类、字段、属性 、方法、事件及所有成员等

 

 

通过Type类获取对象:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace Person
{
    public class PersonC
    {
        public string _age;
        public int _shengao;
        
        public string name
        {
            set;
            get;
        }
        public int age
        {
            get;
            set;
        }
        public string email
        {
            set;
            get;
        }
        public void SayHi()
        { 
            Console.WriteLine("Hello");
        }

         public void test(int a, int b)
         {
               Console.WriteLine(a + b);
         }
    }
}
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using Person;
using System.Reflection;

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

        private void button1_Click(object sender, EventArgs e)
        {

            #region 如何获取一个类型的Type
            //1、通过对象的.GetType方法
            PersonC objPersonc = new PersonC();
            Type type1 = objPersonc.GetType();  //获取PersonC实例的Type

            //2、通过typeof关键字无需实例化类对象就可以拿到类的Type对象
            Type type2 = typeof(PersonC);
            #endregion

            //Type作用
            //1、获取当前类型的父类是谁?
            //Console.WriteLine(type2.BaseType.ToString());
            this.textBox1.Text = "type2类型的父类:" + type2.BaseType.ToString() + Environment.NewLine;
            this.textBox1.AppendText(Environment.NewLine);
            //获取当前类型中字段信息,这里只获取非私有字段,如要获取私有字段需要采取其他方法
            //2、获取指定名称的公有字段
            FieldInfo fields = type1.GetField("_age");
            this.textBox1.AppendText("获取type2类型的_age字段:" + fields.Name+Environment.NewLine);
            this.textBox1.AppendText(Environment.NewLine);
            //3、获取类型中的所有字段
            this.textBox1.AppendText("获取type1类型中的所有字段信息:"+Environment.NewLine);
            FieldInfo[] fileds2 = type1.GetFields();
            for (int i = 0; i < fileds2.Length; i++)
            {
                this.textBox1.AppendText(fileds2[i].Name+Environment.NewLine);
            }
            this.textBox1.AppendText(Environment.NewLine);
            
            //4、获取当前对象中的指定属性
            PropertyInfo infos = type2.GetProperty("name");
            this.textBox1.AppendText("获取type2中指定的name属性名称:"+infos.Name+Environment.NewLine);
            this.textBox1.AppendText(Environment.NewLine);
            //5、获取当前对象中的所有的属性
            this.textBox1.AppendText("获取type2中的所有属性:");
            this.textBox1.AppendText(Environment.NewLine);
            PropertyInfo[] objPro = type2.GetProperties();
            for (int i = 0; i < objPro.Length; i++)
            {
                this.textBox1.AppendText(objPro[i].Name+Environment.NewLine);
            }
            //6、获取当前对象中指定的方法
            this.textBox1.AppendText(Environment.NewLine);
            this.textBox1.AppendText("获取SayHi方法的名称: "+Environment.NewLine);
            MethodInfo objM = type2.GetMethod("SayHi");
            this.textBox1.AppendText(objM.Name+Environment.NewLine)
            //还可以获取对象中的所有方法,事件,接口等

             //获取方法参数类型和参数名称      
            MethodInfo objT = type2.GetMethod("test");
            if (objT != null && objT.GetParameters().Length > 0)
            {
                string parma1type = objT.GetParameters()[0].ParameterType.ToString();
                string parma2type = objT.GetParameters()[1].ParameterType.ToString();

                string parma1 = objT.GetParameters()[0].Name;
                string parma2 = objT.GetParameters()[1].Name;
            }
                
            
        }
    }
}

动态加载程序集,调程序集中类型的方法

 

 调用重载的方法:

 

 

  • 0
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值