2
Answers

Anonymous delegate with anonymous return value

John

John

16y
1.8k
1
I'm wondering if it's possible to have a method that accepts an anonymous delegate invoke on that delegate and have the delegate have a return value and then have that method return the value. 

The requirements are:
1. The anonymous delegate must be able to incapsulate parameters, like the Action<> object.
2. The anonymous delegate must be able to have a return type like: string returnValue = actionMethod.Invoke();

Example of the concept:

public void CallRetrieveDataFunction()
{
    IRandomInterface classMain = new ClassMain();

    Action <IRandomInterface> invokeMethod = delegate(IRandomInterface executeReference)     { executeReference.GetProductByID(productID); };

    object response = RetrieveData(invokeMethod, classMain);
}

public object RetrieveData(Action<T> methodToExecute, U referenceOfObjectImplementingInterface)
{
  object returnValue;
  returnValue = methodToExecute.Invoke(U);
  return returnValue;
}

Anybody have any ideas?
Answers (2)
Next Recommended Forum