Using Action as an argument in C# (mimicking a function pointer)

后端 未结 4 815
眼角桃花
眼角桃花 2021-02-06 08:05

I need to write a delegate function that can \'wrap\' some while/try/catch code around a basic UDP call to verify the link. I made it work for Func for a function that has no ar

4条回答
  •  悲&欢浪女
    2021-02-06 08:28

    Are you trying to do this ...

    protected void udpCommand(Action command, T value)
    {
       while(!linkDownFail)
       {
        try                
        {
          command(value);
          // etc.
        }
      }
    }
    

    Then it would work like this ...

    public void ActionWithInt( int param )
    {
       // some command
    }
    
    Action fp = ActionWithInt;
    
    udpCommand( fp, 10 );  // or whatever.
    

提交回复
热议问题