-1

I want to pass a method from type (class) like a parameter to another method, I don't know how to do this.

    class Program
    {
        static void Main(string[] args)
        {
            MyMethod(Foo.MethodA);
            MyMethod(Foo.MethodB);
        }

        public void MyMethod(???)
        {
        }
    }

    public class Foo
    {
        public void MethodA()
        {
        }

        public int MethodB(int val)
        {
            return val;
        }
    }

I want to do just that

    MyMethod(Foo.MethodA);

I don't want to pass method from a object but just from type (class)

JulioGold
  • 9
  • 3
  • 2
    Use a [`Func`](https://learn.microsoft.com/en-us/dotnet/api/system.func-2?view=net-5.0) type – Rufus L May 12 '21 at 20:44
  • Look up `Delegate`. The `Func` types (and the similar `Action` types) that @RufusL talks about are delegates. – Flydog57 May 12 '21 at 20:47
  • https://stackoverflow.com/q/2082615/1070452 – Ňɏssa Pøngjǣrdenlarp May 12 '21 at 20:51
  • 2
    Note that in your case, your `Foo.MethodA()` is a void function that has no parameters, while `Foo.MethodB()` takes an int as a parameter and returns an int. They are not the same type (well, other than they both can be represented as `delegate`). `MethodA` can be represented by an `Action`, while `MethodB` by a `Func`. You'd need to work very hard to implement anything useful in `MyMethod` since the parameters passed in are so different – Flydog57 May 12 '21 at 20:52
  • Does this answer your question? [Pass a method as a parameter](https://stackoverflow.com/questions/4015451/pass-a-method-as-a-parameter) and [C# Passing Function as Argument](https://stackoverflow.com/questions/3622160/c-sharp-passing-function-as-argument) and [C# Passing a method as a parameter to another method](https://stackoverflow.com/questions/5414515/c-sharp-passing-a-method-as-a-parameter-to-another-method) –  May 12 '21 at 20:52
  • I can't use or run the method, I just want to pass the method like a type, those examples isn't what I want – JulioGold May 12 '21 at 21:18
  • But your methods are not interchangeable since they have different input and output types. What do you want to _do_ with the method in `MyMethod`? That will help determine how yo can do it. – D Stanley May 12 '21 at 21:27
  • Does this answer your question? [C# Any function as parameter](https://stackoverflow.com/questions/18338375/c-sharp-any-function-as-parameter) and [C# Pass any method as parameter](https://stackoverflow.com/questions/61486543/c-sharp-pass-any-method-as-parameter) –  May 12 '21 at 21:35
  • @JulioGodl "*I just want to pass the method like a type*" => methods does not have type in the sens of class type, as I know. You can't write: `typeof(Foo.MethodA)`. –  May 12 '21 at 21:35
  • Great, I want to do exactly that operator nameof() do, like that: nameof(Foo.MethodA); nameof(Foo.MethodB); – JulioGold May 13 '21 at 00:17
  • @JulioGodl Thus `nameof` combined to `typeof` should work to find the [MethodInfo](https://learn.microsoft.com/dotnet/api/system.reflection.methodinfo) by code using this duplicate [How to use reflection to call method by name](https://stackoverflow.com/questions/3110280/how-to-use-reflection-to-call-method-by-name) in case it is your goal. Does that fit? `MyMethod(typeof(Foo), nameof(Foo.MethodB));`. –  May 13 '21 at 06:54
  • 2
    @JulioGold You really need to tell us what you want to do with the passed method, so we can figure out how to do what you want. – Orion May 13 '21 at 09:07

1 Answers1

0

I'm not exactly sure what you are trying to do, but you can generalize methods as Delegate.

static void Main(string[] args)
{
    MyMethod(new Action(Foo.MethodA));
    MyMethod(new Func<int,int>(Foo.MethodB));
}

public static void MyMethod(Delegate del)
{
    // Example code
    var methodInfo = del.Method;
    var methodName = methodInfo.Name;
}

You will still need to wrap the method in some delegate like Action or Func<>

Mitko Petrov
  • 311
  • 1
  • 4
  • 12