我想在用户使用这个方法执行某后操作后,可以执行一个自定义的方法,于是想到用委托。delegate void DoSomething();public class Program{
   public static void main(string[] args)
   {
        DoSomeThing do = new DoSomeThing(WriteLog)
        DoIt doit = new Doit();
        Response.Write(doit.GetAllName("*****",do));
   }   public void WriteLog()
   {
      Log.Write(DateTime.Now.ToString());
   }}
public class DoIt
{
   public string GetAllName(string shortName,DoSomething dosomething)
   {
       string fullName = "John" + shortName;
       if(dosomething != null )
       {
           //这里该执行这个委托类型的方法,改怎么写?
       }
   }
}上面的代码就是个大概意思,如有错误请见谅,请朋友帮忙指点一下!

解决方案 »

  1.   

            if (dosomething != null)
            {
                dosomething();
            }
      

  2.   


    delegate void DoSomething(); //定义一个委托
    public DoSomething do; //定义一个委托实例
    public class Program{ public static void main(string[] args) 

       DoSomeThing do += new DoSomeThing(WriteLog);
        DoIt doit = new Doit(); 
        Response.Write(doit.GetAllName("*****",do)); 

    public void WriteLog() 
    {
     Log.Write(DateTime.Now.ToString());
     } 
    } public class DoIt 
    {
     public string GetAllName(string shortName) 

    string fullName = "John" + shortName; if(dosomething != null ) 
    { //这里该执行这个委托类型的方法,改怎么写?  
    do();} 
    }
     }