zoukankan      html  css  js  c++  java
  • 反射和委托的两个例子

    反射和委托平时大家不怎么会用到,看完一段时间过后又遗忘的无影无踪了。今天看到博客园首页有这篇文章,顺便点进去看一下,感觉还不错。贴过来供以后查阅方便,亦能为理解这两概念的学者提供参考。

    引用地址:http://www.cnblogs.com/last/archive/2009/10/31/1593498.html

    闲来无事,写了一段 反射与委托的 Demo。

    我尽量加了注释,

    希望能给还不太了解反射与委托的朋友以启示。


     1using System;
     2using System.Collections.Generic;
     3using System.Linq;
     4using System.Text;
     5using System.Reflection;
     6
     7namespace ThreadDemo
     8{
     9    /// <summary>
    10    /// 反射实例
    11    /// </summary>

    12    class Reflection
    13    {
    14        static void Main(string[] args)
    15        {
    16            Assembly asm = Assembly.Load("ThreadDemo");//加载程序集
    17            Type[] typeInfo = asm.GetTypes();//获取程序集类型数组
    18            foreach (Type types in typeInfo)
    19            {
    20                Console.WriteLine("程序集中类型:{0}", types.Name);
    21                MethodInfo[] mInfo = types.GetMethods();
    22                foreach (MethodInfo information in mInfo)
    23                {
    24                    Console.WriteLine("此类型所有的方法:{0}", information.Name);
    25                }

    26                Console.WriteLine("\n");
    27            }

    28            Console.WriteLine("\n\n");
    29            Console.WriteLine("开始!");
    30
    31            Type inType = typeof(ThreadTest);
    32            object obj = Activator.CreateInstance(inType);
    33            string[] s = "mlg" };//方法的参数
    34            object objName = Activator.CreateInstance(inType, s);//创建指定类型的实例
    35
    36            MethodInfo methodInfo = inType.GetMethod("ThreadMethod");//根据字符串来获取方法信息
    37            // methodInfo.Invoke(obj, null);
    38            methodInfo.Invoke(objName, null);//调用类型中的方法
    39            Console.ReadLine();
    40
    41        }

    42    }

    43}

    44
    45

     1 namespace ThreadDemo
     2 {
     3     /// <summary>
     4     /// 委托
     5     /// </summary>
     6     class Delegate
     7     {
     8         public delegate int testDelegate(int i,int j);//定义委托类型
     9         static void Main(string[] args)
    10         {
    11             Delegate d = new Delegate();
    12             d.DataInfo();
    13         }
    14         public void getDeltegate(testDelegate dg,int i,int j)//委托类型的传参
    15         {
    16             Console.WriteLine(dg(i,j).ToString());//调用被委托的方法
    17             Console.ReadLine();
    18         }
    19         /// <summary>
    20         /// 动态调用委托
    21         /// </summary>
    22         public void DataInfo()
    23         {
    24           //调用委托
    25             getDeltegate(new testDelegate(DelegateInfor),2,3);
    26             getDeltegate(new testDelegate(DelegateInfor2),1,1);
    27             getDeltegate(new testDelegate(DelegateInfor3),-1,-1);
    28         }
    29 
    30         public int DelegateInfor(int i,int j)
    31         {
    32             return i+j;
    33         }
    34         public int DelegateInfor2(int i, int j)
    35         {
    36             return i + j;
    37         }
    38         public int DelegateInfor3(int i, int j)
    39         {
    40             return i + j;
    41         }
    42   }

    以上两段实例,代码量较少,但是麻雀虽小,五脏俱全。

    全当作参考资料吧。

  • 相关阅读:
    HDU 3911 Black And White 分段树 题解
    Haskell 差点儿无痛苦上手指南
    CFileDialog的使用方法简单介绍
    对 dpif_class 结构体的一点认识
    三层架构之基础知识
    五类常见算法小记 (递归与分治,动态规划,贪心,回溯,分支界限法)
    AlertDialog具体解释
    delphi tcp/ip IdTCPServer1实例一
    23种设计模式(15):备忘录模式
    Android APK反编译具体解释(附图)
  • 原文地址:https://www.cnblogs.com/lumnm/p/1593561.html
Copyright © 2011-2022 走看看