委托动态注入一段代码

using System;

using System.Collections.Generic;

using System.Text;

namespace DelegateTest

{

  public class Writer

  {

    public string Text;

    public int Counter;

    public void Dump()

    {

      Console.WriteLine(Text);

      Counter++;

    }

  }

  class Program

  {

    public delegate void Delegate1();

    static void Repeat10Times(Delegate1 somework)

    {

      for(int i=0;i<10;i++)

        somework();

    }

    static void Main(string[] args)

    {

      Writer writer=new Writer();

      writer.Text="Hello";

      Repeat10Times(writer.Dump);

      Console.WriteLine(writer.Counter);

      Console.Read();

    }

  }

}

posted on 2011-05-09 22:59  易步行天  阅读(247)  评论(2编辑  收藏  举报