zoukankan      html  css  js  c++  java
  • 再谈方法的动态调用

    下面代码是今天课程中,有朋友上台做即席演讲时举的一个例子.这个例子不错,概括了动态调用方法的几种情况,包括静态方法,实例方法,方法重载等等

    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;
    using System.Reflection;
    
    namespace ConsoleApplication1
    {
        class Program
        {
            static void Main(string[] args)
            {
                //MyClass obj = new MyClass();
                //Call(obj, "MethodString", "helloworld");
                //Call(obj, "MethodInt", 1,2);
                //Call(obj, "MethodInt", 1, 2 ,3);
                //Call(obj, "MethodNull");
                //Call(obj, "MethodStatic", "string", 123);
                Call(typeof(StaticClass), "MethodStatic", "string", 123);
    
                Console.Read();
            }
    
            static void Call(object obj, string MethodName, params object[]parameters)
            {
                Type[] types = Array.ConvertAll(parameters, s => s.GetType());
                MethodInfo mi = obj.GetType().GetMethod(MethodName, types);
                if (mi == null) return;
                mi.Invoke(obj,parameters);
                //Type[] types= new Type[parameters.l]
            }
    
            static void Call(Type type, string MethodName, params object[] parameters)
            {
                Type[] types = Array.ConvertAll(parameters, s => s.GetType());
                MethodInfo mi = type.GetMethod(MethodName, types);
                if (mi == null) return;
                mi.Invoke(null, parameters);
                //Type[] types= new Type[parameters.l]
            }
        }
    
        public class MyClass
        {
            public void MethodString(string str)
            {
                Console.WriteLine("string:{0}",str);
            }
    
            public void MethodInt(int int1, int int2)
            {
                Console.WriteLine("int:{0}", int1 + int2);
            }
    
            public void MethodInt(int int1, int int2, int int3)
            {
                Console.WriteLine("int:{0}", int1 + int2 + int3);
            }
    
            public void MethodNull()
            {
                Console.WriteLine("Null");
            }
    
            public static void MethodStatic(string str, int integer)
            {
                Console.WriteLine("static:{0} {1}", str, integer);
            }
        }
        class StaticClass
        {
            public static void MethodStatic(string str, int integer)
            {
                Console.WriteLine("static:{0} {1}", str, integer);
            }
        }
    }
    
  • 相关阅读:
    阿里云POLARDB荣膺2019中国数据库年度最佳创新产品
    实时大数据开发难、运维难、应用难?来,一站解决!
    阿里云应用实时监控 ARMS 再升级,支持 Prometheus 开源生态
    一线实践 | 借助混沌工程工具 ChaosBlade 构建高可用的分布式系统
    使用DataX同步MaxCompute数据到TableStore(原OTS)优化指南
    Twitter 宣布抛弃 Mesos,全面转向Kubernetes
    阿里云Kubernetes服务上使用Tekton完成应用发布初体验
    Knative Tracing 介绍
    不断被取代的传统职业:快速发展的智能交互
    bzoj1433: [ZJOI2009]假期的宿舍
  • 原文地址:https://www.cnblogs.com/chenxizhang/p/1685660.html
Copyright © 2011-2022 走看看