c# 反射专题研究

反射提供描述程序集、模块和类型的对象(Type 类型)。 可以使用反射动态地创建类型的实例,将类型绑定到现有对象,或从现有对象中获取类型,然后调用其方法或访问器字段和属性。 如果代码中使用了特性,可以利用反射来访问它们。 有关更多信息,请参阅特性

/***
*	Title:"测试反射" 项目
*		主题:人员信息
*	Description:
*		功能:XXX
*	Date:2022
*	Version:0.1版本
*	Author:Coffee
*	Modify Recoder:
*/

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

namespace Test_Reflection
{
    class PeopleInfo
    {

        private string guid=Guid.NewGuid().ToString();
        public string desc= "人员信息";

        public string Id { get; set; }

        public string Name { get; set; }

        public string Sex { get; set; }

        public int Age { get; set; }

        public string Work { get; set; }


        public string WorkFlow(string opc)
        {
            //1、规划当天内容

            //2、设置当天内容的紧急程度

            //3、根据紧急情况进行处理

            //4、总结一天内容

            return $"工作流处理完成:{opc}";
        }

        public static string Test()
        {
            Console.WriteLine("这是一个测试");
            return "这是一个静态的方法执行完成";
        }

        public PeopleInfo()
        {

        }

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


    }//Class_end

}

    System.Type type = typeof(PeopleInfo);

           
            string str = string.Empty;
            str += $"1-对象类型名称:{type.Name + enterEffect}";
            str += $"2-对象类型的全名:{type.FullName + enterEffect}";
            str += $"3-对象类型的命名空间:{type.Namespace + enterEffect}";
            str += $"4-对象类型是否为抽象类型:{type.IsAbstract + enterEffect}";
            str += $"5-对象类型是否为数组:{type.IsArray + enterEffect}";
            str += $"6-对象类型是否为类:{type.IsClass + enterEffect}";
            str += $"7-对象类型是否为枚举:{type.IsEnum + enterEffect}";
            str += $"8-对象类型是否为接口:{type.IsInterface + enterEffect}";
            str += $"9-对象类型是否为公有:{type.IsPublic + enterEffect}";
            str += $"10-对象类型是否为密封类:{type.IsSealed + enterEffect}";
            str += $"11-对象类型是否为值类型:{type.IsValueType + enterEffect}";
            str += $"12-对象类型的基类类型:{type.BaseType + enterEffect}";
            str += $"13-对象类型的【程序集】字符串:{type.Assembly + enterEffect}";
            str += $"14-对象类型是否是COM对象:{type.IsCOMObject + enterEffect}";
            str += $"15-对象类型的【程序集.命名空间.类名】字符串:{type.AssemblyQualifiedName + enterEffect}";

            System.Reflection.MemberInfo[] type1 = type.GetMember("PeopleInfo");
            if (type1!=null )
            {
                foreach (var item in type1)
                {
                    str += $"成员信息:{item.Name + enterEffect}";
                }
            }
           
          


            ShowInfo(str);

 PeopleInfo peopleInfo = new PeopleInfo();
            peopleInfo.Id = "CK00988";
            peopleInfo.Name = "张一山";
            peopleInfo.Sex = "男";
            peopleInfo.Age = 23;
            peopleInfo.Work = "信息部总监";
            System.Type type1 = peopleInfo.GetType();
            System.Type type2 = peopleInfo.Age.GetType();

            string str = string.Empty;

            
            System.Reflection.MemberInfo[] type3 = type1.GetMember("Name");
            if (type3 != null)
            {
                foreach (var item in type3)
                {
                    str += $"成员名称:{item.Name}   类型:{item.MemberType} 声明此成员的类为:{item.DeclaringType} {enterEffect}";
                }
            }

            System.Reflection.MemberInfo[] type4 = type1.GetMembers();
            if (type4 != null)
            {
                foreach (var item in type4)
                {
                    str += $"成员名称:{item.Name}   类型:{item.MemberType} 声明此成员的类为:{item.DeclaringType} {enterEffect}";
                }
            }



            System.Reflection.ConstructorInfo[] ci = type1.GetConstructors();
            if (ci != null)
            {
                foreach (var item in ci)
                {
                    str += $"构造成员名称:{item.Name} 类型:{item.MemberType} 声明此成员的类为:{item.DeclaringType} 模块:{item.Module} {enterEffect}";
                    System.Reflection.ParameterInfo[] pi111 = item.GetParameters();
                    if (pi111 != null)
                    {

                        for (int i = 0; i < pi111.Length; i++)
                        {
                            str += $"参数{i}是:{pi111[i]} {enterEffect}";
                        }
                    }

                }

                str += enterEffect;
            }

            System.Type type = System.Type.GetType("Test_Reflection.PeopleInfo");

            //通过无参构造创建实例
            object obj =  Activator.CreateInstance(type);
            //将对象转为PeopleInfo类
            PeopleInfo peopleInfo1 = obj as PeopleInfo;

            str += $"描述1:{peopleInfo1.desc} 名称1:{peopleInfo1.Name} 年龄1:{peopleInfo1.Age}{enterEffect}";

            System.Type type111 = typeof(PeopleInfo);
            //通过有参构造创建实例
            dynamic obj2 = Activator.CreateInstance(type111, System.Reflection.BindingFlags.Instance|
                System.Reflection.BindingFlags.Public,null,new object[] {"卓一航",26},null);

            str += $"描述2:{obj2.desc} 名称2:{obj2.Name} 年龄2:{obj2.Age}{enterEffect}";


            System.Reflection.EventInfo[] eventInfo =  type1.GetEvents();
            if (eventInfo!=null)
            {

                foreach (var item in eventInfo)
                {
                    str += $"事件名称:{item.Name} 类型:{item.EventHandlerType} 声明此成员的类为:{item.DeclaringType} 模块:{item.Module} {enterEffect}";
                }
            }

            System.Reflection.FieldInfo fi2 = type1.GetField("desc",System.Reflection.BindingFlags.Instance|System.Reflection.BindingFlags.Public);
            str += $"修改前字段的值:{fi2.GetValue(peopleInfo)} {enterEffect}";
            fi2.SetValue(peopleInfo,"修改后的值");
            str += $"修改后字段的值:{fi2.GetValue(peopleInfo)} {enterEffect}";

            System.Reflection.FieldInfo[] fi = type1.GetFields(System.Reflection.BindingFlags.Instance | 
                System.Reflection.BindingFlags.NonPublic | System.Reflection.BindingFlags.Public);
            if (fi != null)
            {

                foreach (var item in fi)
                {

                    str += $"字段名称:{item.Name} 类型:{item.FieldType} 声明此成员的类为:{item.DeclaringType} 模块:{item.Module} 值:{item.GetValue(peopleInfo)} {enterEffect}";
                    
                }
            }

            System.Reflection.PropertyInfo[] pi = type1.GetProperties();
            if (pi != null)
            {

                foreach (var item in pi)
                {
                    str += $"属性名称:{item.Name} 类型:{item.MemberType} 声明此成员的类为:{item.DeclaringType} 模块:{item.Module} {enterEffect}";
                }
            }

            System.Reflection.MethodInfo[] mi = type1.GetMethods(System.Reflection.BindingFlags.Instance| System.Reflection.BindingFlags.NonPublic
                );
            if (mi != null)
            {
                foreach (var item in mi)
                {
                    str += $"方法名称:{item.Name} 类型:{item.MemberType} 声明此成员的类为:{item.DeclaringType} 模块:{item.Module} {enterEffect}";
                }
            }

            //反射调用方法一
            string result = (string)type.InvokeMember("WorkFlow", System.Reflection.BindingFlags.InvokeMethod | 
                System.Reflection.BindingFlags.NonPublic | System.Reflection.BindingFlags.Instance,
                null, obj, new object[] { "测试传入的参数11111" });
            str += $"反射调用方法一:{result} {enterEffect}";

            //反射调用方法二
            System.Reflection.MethodInfo mi222 =type.GetMethod("WorkFlow",
                System.Reflection.BindingFlags.NonPublic | System.Reflection.BindingFlags.Instance 
                | BindingFlags.IgnoreCase);

            string result2 = (string)mi222.Invoke(obj,new object[] {"测试传入的参数22222" });

            str += $"反射调用方法二:{result2} {enterEffect}";

            //反射调用方法三
            System.Reflection.MethodInfo mi333 = type.GetMethod("Test");
            string result3 = (string)mi333.Invoke(null,null);

            str += $"反射调用静态方法:{result3} {enterEffect}";

            ShowInfo(str);

  System.Type type = System.Type.GetType("Test_Reflection.PeopleInfo");

            ShowInfo(type);

  var a = new PeopleInfo("wocao", 45);

            var props = a.GetType().GetProperties(BindingFlags.Instance | BindingFlags.Public);
            foreach (var prop in props)
            {
                object value = prop.GetValue(a);
                var ddsaf = prop.Name;
            }

链接:示例项目
提取码:5zzk 

msdn-反射官方文档

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值