C#反射实例_C#里反射的使用方法
2017-10-11 14:05:58  By: shinyuu

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


一、反射是干什么的?

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


二、什么是程序集?

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


三、用到的情况有哪些?

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


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

(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;
        }
    }
}


(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();
        }
    }
}


五、总结

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

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

若资源对你有帮助、浏览后有很大收获、不妨小额打赏我一下、你的鼓励是维持我不断写博客最大动力

想获取DD博客最新代码、你可以扫描下方的二维码、关注DD博客微信公众号(ddblogs)

或者你也可以关注我的新浪微博、了解DD博客的最新动态:DD博客官方微博(dwtedx的微博)

如对资源有任何疑问或觉得仍然有很大的改善空间、可以对该博文进行评论、希望不吝赐教

为保证及时回复、可以使用博客留言板给我留言: DD博客留言板(dwtedx的留言板)

感谢你的访问、祝你生活愉快、工作顺心、欢迎常来逛逛


快速评论


技术评论

DD记账
top
+