2021-04-14

本文介绍了C#中反射的基本应用,包括在运行时获取和操作类的元数据,如改变类的属性值,动态调用方法。还展示了如何根据用户输入动态执行类的方法,并演示了如何通过字符串创建并执行类的方法。此外,还提到了使用Assembly动态加载类实例的方式,以及在接口约束下实现不同类的方法调用。
摘要由CSDN通过智能技术生成

C#学习总结—反射的基本应用

作用1,在运行时操作某个类型的元数据

两种方法实现
实例.GetType()
typeof(类型)
注:一个类型只有一个type(反射能打破类封装的限制)

 class A
        {

        }
var a = new A();
var type1 = typeof(A);
var type2 = a.GetType();

改变类中的属性—通过字符串配置调用属性

   class A
        {
            public string MyProperty { get; set; }

            public void Function(int a)
            {
                Console.WriteLine(a);
            }
        }
        static void Main(string[] args)
        {
            var a = new A();
            var type1 = typeof(A);
            var type2 = a.GetType();
            //调用属性
            var property = type1.GetProperty("MyProperty");
            property.SetValue(a, "aaa", null);
            Console.WriteLine(a.MyProperty);
            //调用方法
            var function = type1.GetMethod("Function");
            function.Invoke(a,new object[] {1});
            Console.Read();
        }

作用2,用户输入A,则执行A方法,用户输入B,则执行B方法

不用反射的做法

 class A
        {
            public string MyProperty { get; set; }
            public void Function(int a) {Console.WriteLine(a);}
            public void A1() { }
            public void B1() { }
        }
        static void Main(string[] args)
        {
            string input=Console.ReadLine();
            var a = new A();
            switch (input.ToLower())
            {
                case "a":
                    a.A1();
                    break;
                case "b":
                    a.B1();
                break;
            }
        }

使用反射的做法—通过字符串配置调用方法

 class A
        {
            public string MyProperty { get; set; }

            public void Function(int a)
            {
                Console.WriteLine(a);
            }
            public void A1() { Console.WriteLine("In function A1"); }
            public void B1() { Console.WriteLine("In function B1"); }
        }
        static void Main(string[] args)
        {
            string input=Console.ReadLine();
            var a = new A();
            var type1 = typeof(A);
            var property = type1.GetProperty("MyProperty");
            property.SetValue(a, "aaa", null);
            var function = type1.GetMethod(input);
            function.Invoke(a, null);
            Console.Read();
        }

根据字符串创建类—在类上通过字符串加载方法
Assembly.Load(“程序集名称”).CreateInstance(“命名空间.类名”)—配合接口使用,封闭变化

 interface MyInterface
        {
            void A1();
            void B1();
        }
        public class A: MyInterface
        {
            public string MyProperty { get; set; }

            public void Function(int a)
            {
                Console.WriteLine(a);
            }
            public void A1() { Console.WriteLine("In function A.A1"); }
            public void B1() { Console.WriteLine("In function A.B1"); }
        }
        public class B : MyInterface
        {
            public string MyProperty { get; set; }

            public void Function(int a)
            {
                Console.WriteLine(a);
            }
            public void A1() { Console.WriteLine("In function B.A1"); }
            public void B1() { Console.WriteLine("In function B.B1"); }
        }
    class Program
    {
        static void Main(string[] args)
        {
            string input1=Console.ReadLine();
            string input2 = Console.ReadLine();
            var instance = (MyInterface)System.Reflection.Assembly.Load("委托事件").CreateInstance("委托事件."+input1);
            var function = instance.GetType().GetMethod(input2);
            function.Invoke(instance, null);
            Console.Read();
        }
    }
  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 2
    评论
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值