C#知识点-反射

一、开发环境

操作系统:Win7

编译器:VS2010

.net版本:.net4.0

二、项目结构

image

三、开发流程

0.编写实体类

namespace ReflectDemo
{
    public class Bird
    {
        public string _id;

        public string Name { get; set; }

        public int Age { get; set; }

        public void Eat()
        {
            Console.WriteLine("我是个吃货");
        }

        public void Eat(string birdName)
        {
            Console.WriteLine("我和" + birdName + "都是个吃货");
        }

        public Bird()
        {

        }

        public Bird(string name, int age)
        {
            this.Name = name;
            this.Age = age;
        }

        public void BirdIntroducion()
        {
            Console.WriteLine("我叫" + Name + ",我" + Age + "岁了");
        }
    }
}

1.获取Assembly对象

namespace ReflectDemo
{
    public class GetAssembly
    {
        public void MethodGetAllAssembly()
        {
            //获取当前应用程序域中的Assembly
            Assembly[] assemblies = AppDomain.CurrentDomain.GetAssemblies();
            Console.WriteLine(assemblies.ToString());
        }

        public void MethodGetCurrentObjAssembly()
        {
            //获取当前对象所在的Assembly
            Assembly assembly = this.GetType().Assembly;
            Console.WriteLine(assembly.ToString());
        }

        public void MethodGetFromBin()
        {
            //获取bin目录下的指定Assembly
            Assembly assembly = Assembly.LoadFrom("ReflectDemo.exe");
            Console.WriteLine(assembly.ToString());
        }
    }
}

2.获取Type对象

namespace ReflectDemo
{
    public class GetType
    {
        public void MethodGetByClassName()
        {
            //通过类名获得Type
            Type type = typeof(Bird);
            Console.WriteLine(type.ToString());
        }

        public void MethodGetByObjName()
        {
            //通过对象名获得Type
            Bird bird = new Bird();
            Type type = bird.GetType();
            Console.WriteLine(type.ToString());
        }

        public void MethodGetByFullName()
        {
            //通过 命名空间.类名获取
            Assembly assembly = this.GetType().Assembly;
            Type type = assembly.GetType("ReflectDemo.Bird");
        }

        public void MethodGetAll()
        {
            //获取Assembly中所有的类型
            Assembly assembly = this.GetType().Assembly;
            Type[] types = assembly.GetTypes();
        }

        public void MethodGetAllPublic()
        {
            //获取Assembly中定义的所有public类
            Assembly assembly = this.GetType().Assembly;
            Type[] types = assembly.GetExportedTypes();
        }
    }
}

3.获取Type成员对象

3.1获取字段信息

namespace ReflectDemo
{
    public class GetFieldInfo
    {
        public void MethodGetPublicField()
        {
            Bird bird = new Bird()
            {
                _id = "1"
            };
            Type type = bird.GetType();
            FieldInfo idInfo = type.GetField("_id");
            string id = idInfo.GetValue(bird).ToString();
            Console.WriteLine(id);
            idInfo.SetValue(bird, "2");
            string newId = bird._id;
            Console.WriteLine(newId);
        }
    }
}

3.2获取属性信息

namespace ReflectDemo
{
    public class GetPropertyInfo
    {
        public void MethodGetAllPublic()
        {
            Bird bird = new Bird()
            {
                Name = "小黄"
            };
            Type type = bird.GetType();
            PropertyInfo nameInfo = type.GetProperty("Name");
            string name = nameInfo.GetValue(bird, null).ToString();
            Console.WriteLine(name);
            nameInfo.SetValue(bird, "小黄黄", null);
            Console.WriteLine(bird.Name);
        }
    }
}

3.3获取方法信息

namespace ReflectDemo
{
    public class GetMethodInfo
    {
        public void MethodGetWithNoParas()
        {
            Bird bird = new Bird();
            Type type = bird.GetType();
            MethodInfo eatMethodInfo = type.GetMethod("Eat", new Type[] { });
            eatMethodInfo.Invoke(bird, null);
        }

        public void MethodWithParas()
        {
            Bird bird = new Bird();
            Type type = bird.GetType();
            MethodInfo eatMethodInfo = type.GetMethod("Eat", new Type[] { typeof(string) });
            eatMethodInfo.Invoke(bird, new object[] { "小黑" });
        }
    }
}

3.4获取构造函数

namespace ReflectDemo
{
    public class GetConstructorInfo
    {
        public void MethodGetActivator()
        {
            Type type = typeof(Bird);
            Bird bird = Activator.CreateInstance(type, new object[] { "小白", 3 }) as Bird;
            bird.BirdIntroducion();
        }

        public void MethodGetConstructor()
        {
            Type type = typeof(Bird);
            ConstructorInfo ctor = type.GetConstructor(new Type[] { typeof(string), typeof(int) });
            Bird bird = ctor.Invoke(new object[] { "小黑", 5 }) as Bird;
            bird.BirdIntroducion();
        }
    }
}

4.编写控制台程序

namespace ReflectDemo
{
    class Program
    {
        static void Main(string[] args)
        {
            Console.WriteLine("---获取Assembly---");
            GetAssembly getAssembly = new GetAssembly();
            getAssembly.MethodGetAllAssembly();
            getAssembly.MethodGetCurrentObjAssembly();
            getAssembly.MethodGetFromBin();

            Console.WriteLine("
---获取Type对象---");
            GetType getType = new GetType();
            getType.MethodGetByClassName();
            getType.MethodGetByObjName();
            getType.MethodGetByFullName();
            getType.MethodGetAll();
            getType.MethodGetAllPublic();


            Console.WriteLine("
---获取字段信息---");
            GetFieldInfo getFieldInfo = new GetFieldInfo();
            getFieldInfo.MethodGetPublicField();

            Console.WriteLine("
---获取属性信息---");
            GetPropertyInfo getPropertyInfo = new GetPropertyInfo();
            getPropertyInfo.MethodGetAllPublic();

            Console.WriteLine("
---获取方法信息---");
            GetMethodInfo getMethodInfo = new GetMethodInfo();
            getMethodInfo.MethodGetWithNoParas();
            getMethodInfo.MethodWithParas();

            Console.WriteLine("
---获取构造函数信息---");
            GetConstructorInfo getConstructorInfo = new GetConstructorInfo();
            getConstructorInfo.MethodGetActivator();
            getConstructorInfo.MethodGetConstructor();

            Console.ReadKey();
        }
    }
}

四、项目说明

1.什么是反射:

(1).在程序运行时,
动态 获取 加载程序集
动态 获取 类型(类,接口)
动态 获取 类型的成员 信息(字段,属性,方法)
(2).在运行时,
动态 创建类型实例,以及 调用 和访问 这些 实例 成员

程序集(Assembly对象)===》类,接口(Type对象)===》类的成员(**Info)

五、其他信息

更多精彩内容请看:http://www.cnblogs.com/2star
原文地址:https://www.cnblogs.com/kimisme/p/5280873.html