Using one delegate for multiple methods with different parameters

Is it possible to somehow use one delegate for several methods with different parameters? I use reflection to get all the methods in the class, and I want to assign each of them a delegate and save that delegate in a dictionary listing as a key. This is if I work to implement the remote calling procedure, so the enumeration is a command associated with the method.

Best regards / Per

+3
source share
5 answers

You need a single way to pass parameters to them. This means that you need to pass them an array of parameters. And that means you need MethodInfo, not a delegate.

MethodInfo . , , Invoke , .

+1

Delegate.CreateDelegate (Action<> Func<>) MethodInfo, MethodInfo.

+1

, , .

, - .

0

, , BCL EventHandler EventArgs:

/ , :

class FooArgs { ... }

delegate void FooDelegate(FooArgs args);

, , FooArgs, .

( , , , , "", , , , . )

0

Dictionary<MyEnum, Object>;. , , .

- , :

interface ICommand
{
    void Execute();
}

class SomeCommand : ICommand
{
    public SomeCommand(/* instance and parameters go here */) { /* ... */ }

    public void Execute() { /* ... */ }
}

class SomeOtherCommand : ICommand { /* ... */ }

, , , w factory w/static :

class RemoteCommand
{
    public static SomeCommand SomeCommand
    {
        get
        {
            var result = new SomeCommand(/* ... */);
            // ...
            return result;
        }
    }

    public static SomeOtherCommand SomeOtherCommand { get { /* ... */ } }
}

Edit:

, , , , , , , :

interface IRemoteCommand
{
    void RemoteMethod();
    void OtherRemoteMethod(/* params */);
    void ImplementedAgainstADifferentServerMethod();
}
0

Source: https://habr.com/ru/post/1733740/


All Articles