P
P
Pragma Games2022-01-29 18:56:09
C++ / C#
Pragma Games, 2022-01-29 18:56:09

Is it possible to add a method to a delegate that was received via an out parameter?

Hello. A bit of pseudocode. In one class, I have a dictionary with delegates, and a method that finds the desired delegate, this class also invokes these delegates

public bool TryFind(string name, out Action action)
{
       action = dictionary[name];
}


Another class method can take the desired delegate and add a method
public bool AddMethod()
{
      if(TryFind("a", out var value)
      {
            value += methodA;
      }
}

Of course, such code does not work, because after assigning a new method, the delegate object will change. How it is possible to solve such trabl? It is clear that after changing the delegate, you can insert a new delegate in the place where we got it from, but maybe there is an easier way.

Answer the question

In order to leave comments, you need to log in

1 answer(s)
@
@insighter, 2022-01-29
_

With this approach, you have a class encapsulation violation.
Through the methods of the class in which the delegates are stored and you need to add methods to the delegates.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question