【问题标题】:Passing a partial Method as a parameter, join then run method传递一个部分方法作为参数,加入然后运行方法
【发布时间】:2015-05-05 15:38:00
【问题描述】:

抱歉,如果这听起来很复杂,我今天的词汇还不够完善。

我有一个方法要使用 .click 示例

middle.click();

但我还有一个

end.click();

如果我想将“middle”或“end”作为参数传递怎么办,是否可以这样做

MethodGo(string usedforSomethingElse, Func<string> thisMethod)
{
     thisMethod.click();
}

【问题讨论】:

    标签: c# methods parameters


    【解决方案1】:

    它应该看起来更像这样:

    MethodGo(string usedforSomethingElse, ISomeObjectWithClickMethod thisObject)
    {
         thisObject.click();
    }
    

    或者,您可以这样做:

    MethodGo(string usedforSomethingElse, Func<string> thisMethod)
    {
         thisMethod();
    }
    

    【讨论】:

      【解决方案2】:
              using System;
              using System.Collections.Generic;
              using System.Linq;
              using System.Text;
              using System.Threading.Tasks;
      
              namespace Student
              {
                  public interface IMyClick
                  {
                      string click();
                  }
              }
          --------------------------
      
              using System;
              using System.Collections.Generic;
              using System.Linq;
              using System.Text;
              using System.Threading.Tasks;
      
              namespace Student
              {
                public  class Middle : IMyClick
                  {
      
                      public string click()
                      {
                          return "Middle Click";
                      }
                  }
              }
      
          ---------------------------
          using System;
          using System.Collections.Generic;
          using System.Linq;
          using System.Text;
          using System.Threading.Tasks;
      
          namespace Student
          {
           public class End :IMyClick
              {
                  public string click()
                  {
                      return "End Click";
                  }
              }
          }
      
      using System;
      using System.Collections.Generic;
      using System.Linq;
      using System.Text;
      using System.Threading.Tasks;
      using Student;
      
      namespace ConsoleApplication1
      {
          class Program
          {
              static void Main(string[] args)
              {
      
                  IMyClick m1 = new Middle();
      
                  IMyClick e1 = new End();
      
                  string result = MethodtoGo(m1);
                  Console.WriteLine(result);
                  Console.Read();
      
              }
      
              static string MethodtoGo(IMyClick cc)
              {
                   return cc.click();
              }
          }
      }
      

      现在您可以在上面的代码中传递 Middle 或 End 类实例,因为它们都实现了相同的接口。 字符串结果 = MethodtoGo(m1); MethodToGo 有一个接口类型的参数,这意味着任何实现接口的类都可以作为输入传递给方法。

      希望这会有所帮助。

      【讨论】:

        猜你喜欢
        • 2018-08-08
        • 2021-04-23
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2013-07-01
        • 1970-01-01
        相关资源
        最近更新 更多