-3

I had asked this question before, but I didn't do a very good job explaining so here I go again...

I have a class something like this

public class BaseClass {
    public void Setup() { Do something... }
    public void TearDown() { Do something else...}
{

public class SubClass : BaseClass {

    public Object1 Method1(Object2 o2) {
        Setup();
        Do something specific to this method...
        TearDown();
        return object1;
    }

    public Object3 Method2(Object4 o4, Object5 o5) {
        Setup();
        Do something different here...
        TearDown();
        return object3;
    }
}

There are other classes that extend BaseClass and the methods in those classes also have to run Setup and TearDown in a similar way.

What I would like to do is not have to call the Setup and TearDown for every method I create. I would like to simply do something like this:

public class BaseClass {
    [RunBeforeEveryMethod]
    public void Setup() { Do something... }
    [RunAfterEveryMethod]
    public void TearDown() { Do something else...}
{

public class SubClass : BaseClass {

    public Object1 Method1(Object2 o2) {
        Do something specific to this method...
        return object1;
    }

    public Object3 Method2(Object4 o4, Object5 o5) {
        Do something different here...
        return object3;
    }
}

I've seen other posts like this and this that are similar. But my situation is a bit different because:

  1. I have to call a method both before and after.
  2. I also have multiple methods with different method names in the sub class where every method has to call these setup and teardown.
  3. The parameters can be anything and any number.
Community
  • 1
  • 1
Steven
  • 714
  • 2
  • 8
  • 21
  • You may have to use an IL Weaving tool like Fody or PostSharp to do this. Attributes are nice, but they don't execute automatically, they are just metadata until they are specifically queried and run. – Ron Beyer Dec 23 '15 at 16:04
  • I've looked at the related post and did some other search of my own. But my situation is different from all the posts I've read. I tried to explain the difference in this post. – Steven Dec 23 '15 at 16:06
  • 1
    pretty sure NUnit does something similar, maybe check out the src http://www.nunit.org/ – mxmissile Dec 23 '15 at 16:07

2 Answers2

2

You can do it using AOP.

I suggest you to look this article http://www.codeproject.com/Articles/8436/Intercepting-method-calls-in-C-an-approach-to-AOSD that do it in runtime.

You can also use IL weaving with something like PostSharp or Fody.

Alberto Monteiro
  • 5,989
  • 2
  • 28
  • 40
1

Yes, you can write a function which does your setup and teardown, taking the "something different" as a parameter.

protected T Wrapper<T>(Func<T> something)
{
    Setup();

    try
    {
        return something();
    }
    finally
    {
        Teardown();
    }
}

Together with lambda functions, you can now do your stuff safely:

public Object1 Method1(Object2 o2) 
{
    return Wrapper<Object1>(() => 
    {
       Do something specific to this method...
       return object1;
    });
}

public Object3 Method2(Object4 o4, Object5 o5) 
{
    return Wrapper<Object3>(() => 
    {
       Do something different here...
       return object3;
    });
}

This technique is used by JavaScript programmers all the time.

  • I liked your answer, but I afraid this question will be closed as duplicate. Could you please add this answer to original question? - http://stackoverflow.com/questions/34436992/is-there-a-generic-way-to-call-another-method-whenever-a-method-is-called-in-c-s/34437062#comment56621175_34437062 – Dzianis Yafimau Dec 23 '15 at 18:41
  • That question already has a similar answer by Aaron, better than this one because it calls TearDown()) even if an exception is thrown. –  Dec 24 '15 at 09:19