zoukankan      html  css  js  c++  java
  • C#动态方法调用


     

    Dynamic Method Invocation

    One very useful feature related to Reflection is the ability to create objects dynamically and call methods on them.
    note : Class1.cs has methods which will be dynamically invoked at runtime from the DynaInvoke.cs

    Class1.cs

    using System;
    class Class1{
           public static String method1()
           {
               return "I am Static method (method1) in class1";
           }
           public String method2()
           {
               return "I am a Instance Method (method2) in Class1";
           }
           public String method3(String s)
           {
              return "Hello " + s;
           }
    }
    save this file as Class1.cs and Compile c:/>csc /t:library Class1.cs

    DynaInvoke.cs

    using System;
    using System.Reflection;
    class DynamicInvoke
    {
    public static void Main(String [] args)
    {

    String path = "Class1.dll"
    Assembly a = Assembly.Load(path);

    //Invoking a static method
    Type mm = a.GetType("Class1");
    String i = (String) mm.InvokeMember ("method1",BindingFlags.Default | BindingFlags.InvokeMethod,null,null,new object [] {}); Console.WriteLine(i);

    //Invoking a non-static method
    object o = Activator.CreateInstance(mm);
    i = (String) mm.InvokeMember("method2",BindingFlags.Default | BindingFlags.InvokeMethod,null,o,new object [] {});
    Console.WriteLine(i);

    //Invoking a non-static method with parameters
    object [] par = new object[] {"kunal"};
    i = (String) mm.InvokeMember("method3",BindingFlags.Default | BindingFlags.InvokeMethod,null,o,par);
    Console.WriteLine(i);
    }
    }

    save this file as DynaInvoke.cs and Compile c:/>csc DynaInvoke.cs and run C:\>DynaIn

     

  • 相关阅读:
    BZOJ4912 [Sdoi2017]天才黑客 【虚树 + 最短路】
    BZOJ2738 矩阵乘法 【整体二分 + BIT】
    BZOJ1185 [HNOI2007]最小矩形覆盖 【旋转卡壳】
    BZOJ4830 [Hnoi2017]抛硬币 【扩展Lucas】
    BZOJ3738 [Ontak2013]Kapitał 【扩展Lucas】
    云计算,大数据,人工智能
    conda基本操作
    nlp基础(一)基本应用
    ml交叉验证
    Python魔术方法
  • 原文地址:https://www.cnblogs.com/chance/p/478172.html
Copyright © 2011-2022 走看看