C# 反射手记

从代码中学习吧

可以把 TestInterface 块里的方法放进 Main 执行来对应的理解

using System;
using System.Collections.Generic;
using System.Linq;
using System.Reflection;
using System.Text;
using System.Threading.Tasks;

namespace ReflectionLearn
{
    class Program
    {
        static void Main(string[] args)
        {
            PrintClassStaticMethodInfo();
        }

        #region TestInterface

        #region GetMembers
        private static void PrintClassPublicInfo(Type varType)
        {
            //返回公开成员;
            MemberInfo[] tempMemInfos = varType.GetMembers();
            ShowMemberConsoleInfo(tempMemInfos);
        }
        private static void PrintClassAllInfo(Type varType)
        {
            //包含非公开、包含实例成员、包含公开;
            //此处含父类的成员;
            MemberInfo[] tempMemInfos = varType.GetMembers(BindingFlags.NonPublic | BindingFlags.Instance | BindingFlags.Public);
            ShowMemberConsoleInfo(tempMemInfos);
        }
        private static void PrintClassSelfInfo(Type varType)
        {
            //包含非公开、包含实例成员、包含公开;
            //BindingFlags.DeclaredOnly 表明 不含父类的成员;
            MemberInfo[] tempMemInfos = varType.GetMembers(BindingFlags.NonPublic | BindingFlags.Instance | BindingFlags.Public | BindingFlags.DeclaredOnly);
            ShowMemberConsoleInfo(tempMemInfos);
        }

        private static void PrintClassSelfInclueStaticInfo(Type varType)
        {
            //包含非公开、包含实例成员、包含公开;
            //BindingFlags.DeclaredOnly 表明 不含父类的成员;
            MemberInfo[] tempMemInfos = varType.GetMembers(BindingFlags.NonPublic | BindingFlags.Instance | BindingFlags.Public | BindingFlags.DeclaredOnly | BindingFlags.Static);
            ShowMemberConsoleInfo(tempMemInfos);
        }
        #endregion

        #region GetFields

        private static void PrintClassFieldsInfo()
        {
            Type tempType = typeof(RefClass);
            RefClass tempRc = new RefClass();
            tempRc.pIntMember = 3;

            FieldInfo[] tempFieInfos = tempType.GetFields(BindingFlags.NonPublic | BindingFlags.Instance | BindingFlags.Public | BindingFlags.DeclaredOnly | BindingFlags.Static);
            foreach (FieldInfo tempInfo in tempFieInfos)
            {
                if (tempInfo.Name == "mIntMember")
                {
                    tempInfo.SetValue(tempRc,999);
                }
                if (tempInfo.Name == "<mIntProperty>k__BackingField")
                {
                    //属性方法;
                    tempInfo.SetValue(tempRc, 777);
                }
                Console.WriteLine("名称: " + tempInfo.Name + "  --  类型: " + GetMemberType(tempInfo.MemberType) + " - 值: " + tempInfo.GetValue(tempRc));
            }
            Console.ReadKey();
        }

        private static void PrintClassPropertyInfo()
        {
            Type tempType = typeof(RefClass);
            RefClass tempRc = new RefClass();
            tempRc.pIntMember = 3;

            PropertyInfo[] tempPropertyInfos = tempType.GetProperties(BindingFlags.NonPublic | BindingFlags.Instance | BindingFlags.Public | BindingFlags.DeclaredOnly | BindingFlags.Static);
            foreach (PropertyInfo tempInfo in tempPropertyInfos)
            {
                MethodInfo tempGetInfo = tempInfo.GetGetMethod(true);
                Console.WriteLine("get方法的名称{0}  返回值类型:{1}  参数数量:{2}  MSIL代码长度:{3} 局部变量数量:{4}", 
                    tempGetInfo.Name, tempGetInfo.ReturnType.ToString(),
                    tempGetInfo.GetParameters().Count(),
                    tempGetInfo.GetMethodBody().GetILAsByteArray().Length, 
                    tempGetInfo.GetMethodBody().LocalVariables.Count);

                MethodInfo tempSetInfo = tempInfo.GetSetMethod(true);
                Console.WriteLine("get方法的名称{0}  返回值类型:{1}  参数数量:{2}  MSIL代码长度:{3} 局部变量数量:{4}", 
                    tempSetInfo.Name, tempSetInfo.ReturnType.ToString(),
                    tempSetInfo.GetParameters().Count(),
                    tempSetInfo.GetMethodBody().GetILAsByteArray().Length,
                    tempSetInfo.GetMethodBody().LocalVariables.Count);

               tempSetInfo.Invoke(tempRc, new object[] { 666 });
               object obj = tempGetInfo.Invoke(tempRc, null);
               Console.WriteLine("方法名:{0}  内部值:{1}", tempInfo.Name, obj);
            }
            Console.ReadKey();
        }

        #endregion

        #region StaticMethods
        private static void PrintClassStaticMethodInfo()
        {
            Type tempType = typeof(RefClass);
            RefClass tempRc = new RefClass();
            tempRc.pIntMember = 3;

            MethodInfo[] tempPropertyInfos = tempType.GetMethods(BindingFlags.NonPublic | BindingFlags.Instance | BindingFlags.Public | BindingFlags.DeclaredOnly | BindingFlags.Static);
            foreach (MethodInfo tempInfo in tempPropertyInfos)
            {
                if (tempInfo.GetParameters().Count() > 0 && tempInfo.GetParameters()[0].ParameterType == typeof(string) )
                {
                    object obj = tempInfo.Invoke(tempRc, new[] { "123" });
                    MethodBody mbody = tempInfo.GetMethodBody();
                    Console.WriteLine("拥有参数的方法名:{0}  返回值类型:{1}  参数1类型:{2}  参数1名称:{3}  方法调用后返回的值:{4}",
                        tempInfo.Name,
                        tempInfo.ReturnType.ToString(),
                        tempInfo.GetParameters()[0].ParameterType.ToString(),
                        tempInfo.GetParameters()[0].Name,
                        obj.ToString());
                }
                else
                {
                    MethodBody mbody = tempInfo.GetMethodBody();
                    Console.WriteLine("没有参数的方法名:{0}  返回值类型:{1}",
                        tempInfo.Name,
                        tempInfo.ReturnType.ToString());
               }
            }
            Console.ReadKey();
        }
        #endregion

        #endregion

        #region LogicBusiness
        private static void ShowMemberConsoleInfo(MemberInfo[] varMemInfos)
        {
            foreach (MemberInfo tempInfo in varMemInfos)
            {
                Console.WriteLine("名称: " + tempInfo.Name + "  --  类型: " + GetMemberType(tempInfo.MemberType));
            }
            Console.ReadKey();
        }


        private static string GetMemberType(MemberTypes varMemType)
        {
            string tempTypeStr = string.Empty;
            switch (varMemType)
            {
                case MemberTypes.Field:
                    {
                        tempTypeStr = "字段";
                    }break;
                case MemberTypes.Method:
                    {
                        tempTypeStr = "方法";
                    }break;
                case MemberTypes.Property:
                    {
                        tempTypeStr = "属性";
                    }break;
                case MemberTypes.Constructor:
                    {
                        tempTypeStr = "构造函数";
                    } break;
                case MemberTypes.Custom:
                    {
                        tempTypeStr = "自定义成员";
                    } break;
                case MemberTypes.Event:
                    {
                        tempTypeStr = "事件";
                    } break;
                case MemberTypes.NestedType:
                    {
                        tempTypeStr = "嵌套";
                    } break;
                case MemberTypes.TypeInfo:
                    {
                        tempTypeStr = "类型";
                    } break;
                default:
                    {
                        tempTypeStr = "未知";
                    }break;
            }
            return tempTypeStr;
        }

        #endregion

    }

    #region DefineInfo

    public class RefClass
    {
        private int mIntMember;
        private int mIntProperty { get; set; }
        protected int mProteInt;
        public int pIntMember { get; set; }

        public CustomClassOne mCustomClass;

        public void AnyOne()
        {

        }
        public static void ThisIsStaticMethodOne()
        {

        }
        public static void ThisIsStaticMethodTwo()
        {

        }

        public static string HadReturnValueA(string varStr)
        {
            int tempIntA;
            int tempIntB;
            return varStr;
        }
        public static string HadReturnValueB(string varStr)
        {
            string tempStr;
            return varStr;
        }
    }

    public class CustomClass
    {
        public int MCustomInt;

    }

    public class CustomClassOne : CustomClass
    {
        public int MCustomInt;
        public static CustomClassTwo mCustomClassTwo;
    }

    public class CustomClassTwo : CustomClass
    {
        public int MCustomInt;

    }

    #endregion

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值