C#中类的反射

C#中类的反射

在我们不知道类内的属性名和方法时,可以通过反射的方式访问类内的属性和方法,操作如下:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Reflection;//引入反射命名空间
using System.Threading.Tasks;
using System.Windows.Forms;

namespace Reflection
{
    static class Program
    {
        /// <summary>
        /// 应用程序的主入口点。
        /// </summary>
        [STAThread]
        static void Main()
        {
            //属性的访问
            //静态属性的访问
            Console.WriteLine("Initial value of StaticProperty:" + Example.StaticProperty);           
            Type exampleType = typeof(Example);
            PropertyInfo examplePropertyInfo = exampleType.GetProperty("StaticProperty");

            //不通过构造对象直接更改静态属性的值
            examplePropertyInfo.SetValue(null, 70);
            Console.WriteLine("New1 value of StaticProperty:"+examplePropertyInfo.GetValue(null));

            //通过构造对象后更改静态属性的值
            Example staticExample = new Example();
            examplePropertyInfo.SetValue(staticExample, 75);
            Console.WriteLine("New2 value of StaticProperty:"+examplePropertyInfo.GetValue(staticExample));

            //实例属性的访问
            Example instanceExample = new Example();
            Console.WriteLine("Initial value of InstanceExample:" + instanceExample.InstanceProperty);
            examplePropertyInfo = exampleType.GetProperty("InstanceProperty");
            examplePropertyInfo.SetValue(instanceExample, 80);
            Console.WriteLine("New value of InstanceExample:"+examplePropertyInfo.GetValue(instanceExample));

            //属性相同的两个类通过遍历属性进行属性赋值
            Type example1Type = typeof(Example1);
            foreach(PropertyInfo item in example1Type.GetProperties())
            {
                Type exampleType1 = typeof(Example);
                Example example = new Example();
                Example1 example1 = new Example1();
                Console.WriteLine("Initial value of "+item.Name+" :" + item.GetValue(example1));
                item.SetValue(example1, exampleType1.GetProperty(item.Name.ToString()).GetValue(example));
                Console.WriteLine("New value of "+item.Name+" :"+item.GetValue(example1));
            }

            //方法的访问
            Example methodExample = new Example();
            Type methodExampleType = methodExample.GetType();
            //静态方法的调用
            MethodInfo staticMethodInfo = methodExampleType.GetMethod("StaticShow");
            staticMethodInfo.Invoke(methodExample, null);
            staticMethodInfo.Invoke(null, null);

            //非静态方法的调用
            MethodInfo noStaticMethodInfo =methodExampleType.GetMethod("NoStaticShow");
            noStaticMethodInfo.Invoke(methodExample,null);

            //带参数方法的调用
            MethodInfo paramMethodInfo = methodExampleType.GetMethod("ParamShow");
            paramMethodInfo.Invoke(methodExample, new object[] { "ParamMethodFunc" });

            //带返回值的方法调用
            MethodInfo returnMethodInfo = methodExampleType.GetMethod("returnAdd");
            object result=returnMethodInfo.Invoke(methodExample, new object[] { 1, 2 });
            Console.WriteLine(result);

            Console.ReadKey();
        }
    }
    class Example
    {
        private static int _staticProperty = 10;
        private int _instanceProperty = 20;

        public static int StaticProperty
        {
            get { return _staticProperty; }
            set { _staticProperty = value; }
        }

        public int InstanceProperty
        {
            get { return _instanceProperty; }
            set { _instanceProperty = value; }
        }

        public void NoStaticShow()
        {
            Console.WriteLine("I am NoStatic Method");
        }

        public static void StaticShow()
        {
            Console.WriteLine("I am Static Mehtod");
        }

        public void ParamShow(string msg)
        {
            Console.WriteLine("I am ParamShow:"+msg);
        }

        public int returnAdd(int a,int b)
        {
            return a + b;
        }
    }

    class Example1
    {
        private static int _staticProperty = 10;
        private int _instanceProperty = 20;

        public static int StaticProperty
        {
            get { return _staticProperty; }
            set { _staticProperty = value; }
        }

        public int InstanceProperty
        {
            get { return _instanceProperty; }
            set { _instanceProperty = value; }
        }
    }
}


运行结果:
在这里插入图片描述

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值