zoukankan      html  css  js  c++  java
  • C#的反射机制调用方法

    .NET的反射(Reflection)是非常完善和强大的,例如有名的.NET反编译工具Red Gate's .NET Reflector就是使用了.NET自身的反射机制,这里有一个比较简单的实例(使用控制台程序),看看.NET中如何使用反射。
     

    1. using System;  
    2. using System.Reflection;  
    3.  
    4. namespace Mengliao.CSharp.C13.S02  
    5. {  
    6.     class MyClass  
    7.     {  
    8.         private int count;  
    9.  
    10.         public MyClass(int value)  
    11.         {  
    12.             count = value;  
    13.         }  
    14.  
    15.         public void m1()  
    16.         {  
    17.             Console.WriteLine("Called method 1.");  
    18.         }  
    19.  
    20.         public static int m2(int x)  
    21.         {  
    22.             return x * x;  
    23.         }  
    24.  
    25.         public void m3(int x, double y)  
    26.         {  
    27.             Console.WriteLine("Called method 3, paramaters: x = {0}, y = {1:E}.", x, y);  
    28.         }  
    29.  
    30.         public void m4()  
    31.         {  
    32.             Console.WriteLine("Called method 4. Count = {0}", count);  
    33.         }  
    34.  
    35.         private static string m5(double x) //私有静态方法,不能直接调用,但可以绑定到委托  
    36.         {  
    37.             return Math.Sqrt(x).ToString();  
    38.         }  
    39.     }  
    40.  
    41.     class Program  
    42.     {  
    43.         public static void Main()  
    44.         {  
    45.             //取得MyClass的Type对象,下面的代码使用Type的静态方法需指明程序集,作用相同  
    46.             //Type t = Type.GetType("Mengliao.CSharp.C13.S02.MyClass, ConsoleApplication, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null");  
    47.             Type t = typeof(MyClass);  
    48.             //通过Activator实例化MyClass,供实例方法调用  
    49.             object obj = Activator.CreateInstance(t, new object[] { 88 });  
    50.  
    51.             MethodInfo[] methods = t.GetMethods(); //获取MyClass的所有方法列表  
    52.  
    53.             foreach (MethodInfo nextMethod in methods) //枚举所有方法  
    54.             {  
    55.                 Console.WriteLine(nextMethod.ToString()); //显示方法信息  
    56.                 if (nextMethod.Name == "m1") //方法m1  
    57.                 {  
    58.                     nextMethod.Invoke(obj, null); //使用obj对象调用方法m1,无参数  
    59.                 }  
    60.                 if (nextMethod.Name == "m2") //方法m2  
    61.                 {  
    62.                     //静态方法,使用null调用方法m2,建立参数数组,传入10  
    63.                     Console.WriteLine("Called static method 2, return {0}", nextMethod.Invoke(null, new object[] { 10 }));  
    64.                 }  
    65.             }  
    66.  
    67.             MethodInfo m3Info = t.GetMethod("m3"); //获取方法m3  
    68.             m3Info.Invoke(obj, new object[] { 123, 0.456 }); //调用方法m3,传入对应的2个参数  
    69.  
    70.             //获取方法m4,使用obj对象调用方法,无参数  
    71.             t.InvokeMember("m4", BindingFlags.InvokeMethod, null, obj, null);  
    72.  
    73.             //建立泛型委托runMe,并绑定MyClass的静态私有方法m5  
    74.             Delegate runMe = Delegate.CreateDelegate(typeof(Func<double, string>), t, "m5");  
    75.             Console.WriteLine("Call delegate with m5: Sqrt(2) = {0}", ((Func<double, string>)runMe)(2)); //调用该委托  
    76.               
    77.             Console.ReadLine();  
    78.         }  
    79.     }  

    使用反射机制可以调用各种方法,包括私有的、静态的等等,程序中的注释非常详细,这里就不多说了。

    本文出自 “梦辽软件工作室” 博客,请务必保留此出处http://mengliao.blog.51cto.com/876134/511804
     

  • 相关阅读:
    苹果开发者账号证书、授权配置文件设置流程
    JQuery 总结(1) 选择器的使用
    PC和移动端自动识别
    前端获取ip的接口
    AJAX 跨域解决办法
    linux
    复制剪切板实现
    css animation 制作打开动画效果
    弹出层播放视频
    盛世龙图项目总结
  • 原文地址:https://www.cnblogs.com/dzone/p/1985843.html
Copyright © 2011-2022 走看看