带你秒懂C#反射机制

前言:反射在日常编程中由于接触较少,所以在大家眼中总是一个神秘的存在,也会觉得反射是一件复杂的事情,今天就在这里帮助大家了解一些反射的基础操作,其实反射很简单!

通过本文你将了解到?

  1. 什么是反射?
  2. 如何反射获取属性?
  3. 如何反射获取访问器?
  4. 如何反射获取方法?
  5. 现成的代码(copy直接使用,甚至不需要理解)以及图文解释。

什么是反射?

反射机制是在运行状态中,对于任意一个类,都能够知道这个类的所有属性和方法,对于任意一个对象,都能够调用它的任意一个方法和属性,是一种动态获取的信息以及动态调用对象的方法的功能。

那么在介绍前先创建个反射的目标类,接下来将通过反射方法获取此类所有数据:

namespace TestReflection
{
    public class TestReflectionClass
    {
        private int privateUnStatic;
        private static int privateStatic;

        public int publicUnStatic;
        public static int publicStatic;

        public int PublicProperty
        {
            set
            {
                privateUnStatic = value;
                Debug.LogError("PublicProperty set 已执行");
            }
            get
            {
                Debug.LogError("PublicProperty Get 已执行");
                return privateUnStatic;
            }
        }

        public void TestFunc()
        {
            Debug.LogError("this is a function!");
        }

        public int TestFuncWithParam(int a, int b)
        {
            int sum = a + b;
            return sum;
        }
    }
}

反射获取属性

获取属性主要是通过Type的GetField()方法来获取,下面案例列举所有情况,请细品~
获取属性代码:

public void TestField()
        {
            TestReflectionClass testCase = new TestReflectionClass()
            {
                publicUnStatic = 5,
            };
            TestReflectionClass.publicStatic = 6;
            //获得指定类型所在程序集
            Assembly testClassAssembly = Assembly.GetAssembly(typeof(TestReflection.TestReflectionClass));
            //获取测试类型
            Type testClass = testClassAssembly.GetType("TestReflection.TestReflectionClass");
            
            //获取public非static属性
            FieldInfo publicUnstaticField = testClass.GetField("publicUnStatic");
            Debug.LogError("publicUnstaticField value:" + publicUnstaticField.GetValue(testCase));
            
            //获取private非static属性
            FieldInfo privateUnstaticField = testClass.GetField("privateUnStatic",BindingFlags.Instance|BindingFlags.NonPublic);
            Debug.LogError("privateUnstaticField value:" + privateUnstaticField.GetValue(testCase));
            
            //获取public static属性
            FieldInfo publicStaticField = testClass.GetField("publicStatic");
            Debug.LogError("publicUnstaticField ByInstance value:" + publicStaticField.GetValue(testCase));
            Debug.LogError("publicUnstaticField ByClass value:" + publicStaticField.GetValue(null));
            
            //获取private static属性
            FieldInfo privateStaticField = testClass.GetField("privateStatic",BindingFlags.Static | BindingFlags.NonPublic);
            Debug.LogError("privateStaticField ByInstance value:" + privateStaticField.GetValue(testCase));
            Debug.LogError("privateStaticField ByClass value:" + privateStaticField.GetValue(null));
            
            privateStaticField.SetValue(null,100);
            Debug.LogError("privateStaticField after Set:" + privateStaticField.GetValue(null));
        }

方法执行后的日志:
带你秒懂C#反射机制_第1张图片

反射获取访问器

获取访问器主要是通过Type的GetProperty()方法来获取,由于和属性的获取有许多相似之处,下面案例列了举部分情况,请细品~
获取访问器代码:

public void TestProperty()
        {
            TestReflectionClass testCase = new TestReflectionClass();
            Type testClass = typeof(TestReflectionClass);
            
            //获取public属性
            PropertyInfo publicProperty = testClass.GetProperty("PublicProperty");
            Debug.LogError("publicProperty value:" + publicProperty.GetValue(testCase));
            publicProperty.SetValue(testCase,66);
            Debug.LogError("publicProperty after set value:" + publicProperty.GetValue(testCase));
            //其他类型属性类似,可参考field,不在介绍
        }

方法执行后的日志:
带你秒懂C#反射机制_第2张图片

反射获取方法

获取方法主要是通过Type的GetMethod()方法来获取,由于和属性的获取有许多相似之处,下面案例列了举部分情况,请细品~
获取方法代码:

public void TestMethod()
        {
            TestReflectionClass testCase = new TestReflectionClass();
            Type testClass = typeof(TestReflectionClass);
            
            //反射调用普通方法
            MethodInfo testFunc = testClass.GetMethod("TestFunc");
            testFunc.Invoke(testCase, null);
            
            //反射调用带参方法
            MethodInfo testFunc1 = testClass.GetMethod("TestFuncWithParam");
            int sum =(int)testFunc1.Invoke(testCase,new object[]{6,7});
            Debug.LogError("sum:"+sum);
        }

方法执行后的日志:
TestMethodLog.png

反射获取特性

Attribute是一个用来为目标添加描述信息的类,它的目标可以是类、方法、属性等。关于特性的介绍,网上有很多https://www.cnblogs.com/hyddd/archive/2009/07/20/1526777.html

特性都是通过反射来获取使用的,获取特性的方法也很简单,可通过特性接口获得Attribute.GetCustomAttributes(Memberinfo element),也可通过对应的反射类获得如Type、MethodInfo、FieldInfo的GetCustomAttributes()方法获取,还是很简单的~

结语:方法中有注释,以及最终的结果图都打印出来了,此篇为反射基础,之后会逐步推出更深入的研究,关注,带你飞!

你可能感兴趣的:(c#,反射)