Include code in every method

  • Thread starter Thread starter Jake K
  • Start date Start date
J

Jake K

I have about 100 methods in an application I'm developing. There will be
many, many more methods added as the application grows. I need to include a
line of code at the start of every method in the project and every method
that will be added in the future. The line of code is simply

if (!bSomeVar)
return;

Basically I have to check this bool var before executing the code in the
moethods.
Is there an easy way to inhrit this line or other options?
 
Jake,

You could try the template method pattern.

public abstract class Abstract
{
private bool m_Flag;

public void DoSomething()
{
if (!m_Flag)
{
DoSomethingConcrete();
}
}

protected abstract void DoSomethingConcrete();
}

public class Concrete
{
protected void DoSomethingConcrete()
{
// Whatever
}
}

I'm not sure this would be feasible in your situation since there would
have to be a template method for each method signature in your
application, but it's the first thing that comes to mind. Aspect
oriented programming might help you as well.

Brian
 
Jake K said:
I have about 100 methods in an application I'm developing. There will be
many, many more methods added as the application grows. I need to include
a line of code at the start of every method in the project and every method
that will be added in the future. The line of code is simply

if (!bSomeVar)
return;

Basically I have to check this bool var before executing the code in the
moethods.
Is there an easy way to inhrit this line or other options?

Not sure how you could do this "easily" but what you can do is a regular
expression search and replace to find all methods or entry points in your
project/solution and replace it with itself + your 'if' statements.

HTH,
Mythran
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Back
Top