c# -反射 初见

反射是一个很强大的功能,不过好像有些消耗性能,大家慎重使用。

1.反射是干什么的?

通过反射,我们可与获取程序集中的原数据。

1.什么是程序集?

dll、exe  这些将很多能实现具体功能的代码封装起来的文件(我自己的理解,可能不对!)。

2.用到的情况有哪些?

编译器的提示功能、反编译、还有调用别人的dll时,其它我不知道的。。

3.下面直接奉上一个实例的代码,供参考。

(1)先建一个叫做Common的类库,在里面建一个叫Person的类,类的代码如下。

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

namespace Common
{
    public class Person
    {
        //姓名
        private string _name;
        public string Name
        {
            get { return _name; }
            set { _name = value; }
        }
        //年龄
        private int _age;
        public int Age
        {
            get { return _age; }
            set { _age = value; }
        }
 
        //打印姓名和年龄
        public void printfNameAge()
        {
            Console.WriteLine(_name);
            Console.WriteLine(_age);
        }

        //有参数的构造函数
        public Person(string name, int age)
        {
            this.Name = name;
            this.Age = age;
        }

    }
}
View Code

(2)在建一个窗体程序,其中的Program.cs代买如下,里面实现了一些反射的常用方法。

在运行这个程序之前,你要将Common编译一下,然后去Debug中将Common.dll拷贝到你建好程序的Debug中。

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Reflection;//反射
//Author  崔时雨
//Date   20160808
namespace 反射
{
    class Program
    {
        static void Main(string[] args)
        {
            //1.加载程序集文件
            string path = AppDomain.CurrentDomain.BaseDirectory + "Common.dll";
            Assembly ass = Assembly.LoadFile(path);

            //2. 获取数据集数据的三个函数
            //2.1 获取指定对象的类型
            Type t = ass.GetType("Common.Person");
            Console.WriteLine(t.Name);

            //2.2 获取元数据,无论公有私有。
            Type[] type1 = ass.GetTypes();
            Console.WriteLine("打印2.2 获取元数据,无论公有私有。");
            foreach (Type item in type1)
            {
                Console.WriteLine(item.Name);
                Console.WriteLine(item.FullName);
                Console.WriteLine(item.Namespace);
            }

            //2.3 值获取公有的
            Type[] type2 = ass.GetExportedTypes();
            Console.WriteLine("
打印2.3 值获取公有的");
            foreach (Type item in type2)
            {
                Console.WriteLine(item.Name);
                Console.WriteLine(item.FullName);
                Console.WriteLine(item.Namespace);
            }

            //3 创建对象
            //3.1调用person中的默认无参的构造函数
            // object obj1 = ass.CreateInstance("Common.Person");//通常不用这种,如果存在有参数的构造函数,不能创建。
            //3.2 可以构造函数有参数的对象
            object obj2 = Activator.CreateInstance(t/*对象类型*/, "小明", 18);
            //3.2.1获得数据源的属性数组 
            PropertyInfo[] strPro = obj2.GetType().GetProperties();
            Console.WriteLine("
打印3.2.1获得数据源的属性数组");
            foreach (PropertyInfo item in strPro)
            {
                Console.WriteLine(item.Name);
            }
            //3.2.2获得数据源的方法数组 
            MethodInfo[] methods = obj2.GetType().GetMethods();
            Console.WriteLine("
打印3.2.2获得数据源的方法数组 ");
            foreach (MethodInfo item in methods)
            {
                Console.WriteLine(item.Name);
            }

            //3.3调用函数,打印。
            MethodInfo md = obj2.GetType().GetMethod("printfNameAge");
            Console.WriteLine("
调用 Person中的printfNameAge方法,打印 姓名和年龄");
            md.Invoke(obj2,null);

            Console.ReadKey();
        }
    }
}
View Code

4.运行结果

总结:

参考了很多人的博客,发现要想弄清楚反射还需要许多其它的知识,使用这篇内容的知识,已经可以简单的使用反射了。

知识点总要一个一个学,积累的多了,就会连成串。

原文地址:https://www.cnblogs.com/Cui-Shi-Yu/p/5750747.html