将对象的任何方法作为函数中的参数传递

标题可能不够清楚,但是我将向您展示一个简短的程序,以使您理解我想做什么:

Class Program
{
   private static Obj A = new Obj(...);

   private static void Function(AnyMethodOfMyObject() m)
   {
      object[] result = A.m();
      ...
   }

   static void main()
   {
      double a,b,c = 0;
      string d = " ";
      Function(MethodX(a,b,c));
      Function(MethodY(d,a,b));
      ...

   }
}

这些方法将始终返回与object []相同的类型,但是我没有相同数量/类型的参数。

谢谢 !

评论
  • 敷衍
    敷衍 回复

    You can do this simply with a Func<object[]>:

    class Program
    {
       private static Obj A = new Obj(...);
    
       private static void Function(Func<object[]> m)
       {
          object[] result = m();
          ...
       }
    
       static void main()
       {
          double a,b,c = 0;
          string d = " ";
          Function(() => MethodX(a,b,c));
          Function(() => MethodY(d,a,b));
          ...
    
       }
    }
    

    Func<object[]> is a delegate which does not take any parameters, and which returns an array of objects.