简体   繁体   中英

Using one delegate to for several methods with different parameters

Is it possible to use one delegate for several methods with different parameters somehow? I use reflection to get all the methods in a class, and I want to assign each of them a delegate and save that delegate in a dictionary with an enum as the key. This if for a remote procedure call implementation I'm working on so the enum is a command associated with a method.

Regards/Per

You'd need a uniform way of passing them parameters. That means you'd need to pass them an array of parameters. And that means you need a MethodInfo , not a delegate.

Just store the MethodInfo against the name in the dictionary. Then when you need to call them, use the Invoke method to make the call, passing the target object and the array of parameters.

您可以使用Delegate.CreateDelegate函数并传递相应的委托类型( Action<>Func<> )和MethodInfo的实例,在这种情况下,您应该根据MethodInfo参数构造委托类型。

If the parameter count, order and type are the same, yes.

Simply put, a delegate is just a method signature.

Something you could try is similar to the BCL's EventHandler delegate and the EventArgs parameter class:

Instead of directly passing all arguments to the delegate / method as parameters, you put them inside an object of some known base type and then you pass that object to the delegate:

class FooArgs { ... }

delegate void FooDelegate(FooArgs args);

Then, for all your methods requiring certain parameters, derive a new class from FooArgs that contains all the expected parameters.

(While this may seem like a detour, it's probably the only option since all your methods in the dictionary need to have the same "signature" as the delegate, ie same number of parameters, same parameter types, same return type.)

You could make a Dictionary<MyEnum, Object> ;. After you extract the value, cast it to the appropriate delegate and invoke it.

Alternately, you could use something like the command pattern, and pass in commands rather than enumerated values:

interface ICommand
{
    void Execute();
}

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

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

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

If you want them to be accessible like an enum, you could make a factory class w/ static methods to create your specific commands:

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

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

Edit:

Also, not sure of your needs, but exposing a list of method calls that the user might want to make, but abstracting out exactly what gets called sure sounds like an interface:

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM