【问题标题】:quick-and-dirty way to pass a method with parameters, as a parameter?将带参数的方法作为参数传递的快速而肮脏的方法?
【发布时间】:2011-01-15 14:06:23
【问题描述】:

让我先说一下我对 C# 非常熟悉的事实。话虽这么说,我正在寻找一种方法来传递带有参数的方法作为参数。 理想情况下,我想做的是:

static void Main(string[] args)
{
    methodQueue ( methodOne( x, y ));
}

static void methodOne (var x, var y)
{
    //...do stuff
}

static void methodQueue (method parameter)
{
    //...wait
    //...execute the parameter statement
}

谁能指出我正确的方向?

【问题讨论】:

    标签: c# methods parameters


    【解决方案1】:

    您可以使用Action delegate 传递返回void 的参数化方法。 然后你可以这样做:

    
    public void Main()
    {
        MethodQueue(MethodOne);
    } 
    public void MethodOne(int x, int y) 
    {
        // do stuff
    }
    public void MethodQueue(Action<int, int> method)
    {
        // wait
        method(0, 0);
    }
    
    如果您的方法需要返回一个值,您也可以使用 Func 委托。

    【讨论】:

      【解决方案2】:

      Use an Action delegate

      // Using Action<T>
      
      using System;
      using System.Windows.Forms;
      
      public class TestAction1
      {
         public static void Main()
         {
            Action<string> messageTarget; 
      
            if (Environment.GetCommandLineArgs().Length > 1)
               messageTarget = ShowWindowsMessage;
            else
               messageTarget = Console.WriteLine;
      
            messageTarget("Hello, World!");   
         }      
      
         private static void ShowWindowsMessage(string message)
         {
            MessageBox.Show(message);      
         }
      }
      

      【讨论】:

        【解决方案3】:

        这应该做你想做的事。它实际上是将无参数方法传递给您的函数,但 delegate(){methodOne( 1, 2 );} 正在创建一个匿名函数,该函数使用适当的参数调用 methodOne。

        我想在输入之前对其进行测试,但我的方法只有 .net framework 2.0。

        public delegate void QueuedMethod();
        
        static void Main(string[] args)
        {
            methodQueue(delegate(){methodOne( 1, 2 );});
            methodQueue(delegate(){methodTwo( 3, 4 );});
        }
        
        static void methodOne (int x, int y)
        {
        
        }
        
        static void methodQueue (QueuedMethod parameter)
        {
            parameter(); //run the method
            //...wait
            //...execute the parameter statement
        }
        

        【讨论】:

        • 我的问题的所有答案都足够了,但是您的回答帮助代表们点击了我,所以我接受了您的回答。谢谢大家。
        【解决方案4】:

        您也可以使用纯 C 函数指针的替代方案,但这可能会有点混乱,尽管它工作得很好。

        【讨论】:

          【解决方案5】:

          使用 lambda 语法,这几乎总是最简洁的方式。请注意,这在功能上等同于匿名委托语法

           class Program
           {
              static void Main(string[] args)
              {
                  MethodQueue(() => MethodOne(1, 2));
              }
          
              static void MethodOne(int x, int y)
              {...}
          
              static void MethodQueue(Action act)
              {
                  act();
              }
          
          
           }
          

          【讨论】:

            猜你喜欢
            • 2015-11-24
            • 2010-09-15
            • 1970-01-01
            • 2013-06-09
            • 2018-10-26
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            相关资源
            最近更新 更多