-2

I have two function which have some common functionality (i.e. to make connection with service and close connection after calling). I made a method named "InvokeService" with parameter of Func in it.How can I get parameters of request in InvokeService? I mean I need to get the object value of request? You can clear be clear by my demo code given below:

public void Method1(){
    InvokeService(()=> _service.getMathod1(request);
 }
 public void Method2(){
    InvokeService(()=> _service.getMathod2(request);
 }
 public void InvokeService(Func<T> request){
     //service open
     //I need here a complete object of the request of Method2 and its parameters
    request.Invoke();
    //service close
 }

If any thing ambiguous or not understandable feel free to ask me.

Azaz ul haq
  • 53
  • 10
  • 9
    It's not clear what your question actually is. There are no question marks in your post. – canton7 Nov 19 '19 at 13:39
  • 2
    That's great! What is your question ? – Fabjan Nov 19 '19 at 13:40
  • I think what you're looking for is a [Code Review](https://codereview.stackexchange.com/). – Aly Elhaddad Nov 19 '19 at 13:43
  • You might need to declare it like `InvokeService(Func request)`, in order for `InvokeService` to know about `T`, but other then that, yeah... what is your question? – Corak Nov 19 '19 at 13:43
  • Btw. the last item of the generic parameter list of a `Func<...>` determines the *return type*. So a `Func` *returns* a `T`. It's basically what a "normal" method like `public T doStuff() { ... }` would look like. A `Func` would *expect* a parameter `T1` and would *return* a `T2`; similar to `public T2 doStuff(T1 t1) { ... }`. For a `void` return, you might want to use `Action<...>`. -- Or what do you mean with "I need here a complete object of the request of Method2 and its parameters" exactly? – Corak Nov 19 '19 at 13:50
  • I updated the requirements – Azaz ul haq Nov 19 '19 at 14:02

2 Answers2

0

You may want to use the template method pattern:

Define the skeleton of an algorithm in an operation, deferring some steps to subclasses. Template Method lets subclasses redefine certain steps of an algorithm without changing the algorithm's structure.

In your case, you can have something like this:

    public abstract class AbstractClass
    {
        protected abstract void PrimitiveOperation();

        public void TemplateMethod()
        {
            // before common functionality

            PrimitiveOperation();

            // after common functionality
        }
    }

    class ConcreteClassA : AbstractClass
    {
        protected override void PrimitiveOperation()
        {
            // your A logic
        }
    }

    class ConcreteClassB : AbstractClass
    {
        protected override void PrimitiveOperation()
        {
            // your B logic
        }
    }

If you want to return something different for each concrete class or have a different parameter depending the concrete class, you can achieve that with generics. Let me know if that is the case.

joacoleza
  • 775
  • 1
  • 9
  • 26
0

It can be solve by using Reflection; request.GetMethodInfo()

Azaz ul haq
  • 53
  • 10