How to do something automatic before call a method?


Hi ,All

I have a simple class that contain few method, like this

public class TestClass{

public void A(){}
public void B(){}

}

I want to do something automatic before call the method ( for
example ,A() or B() etc)。Is possible to do this through pass an
attribute or other something, except write the code like below:

public void A(){
DoSomethingFirst();
//continue to do method logic
}
public void B(){
DoSomethingFirst();
//continue to do method logic
}

I think the attribute maybe do this , or the event is the better
choice, but how can we handler the event when the Method(A() or B())
be invoked.

Any advise is welcome, thank you!

best regard,
Ares
 
Ad

Advertisements

P

Pavel Minaev

I have a simple class that contain few method, like this

public class TestClass{

public void A(){}
public void B(){}

}

I want to do something automatic before call the method ( for
example ,A() or B() etc)Is possible to do this through pass an
attribute or other something, except write the code like below:

public void A(){
DoSomethingFirst();
//continue to do method logic}

public void B(){
DoSomethingFirst();
//continue to do method logic

}

I think the attribute maybe do this , or the event is the better
choice, but how can we handler the event when the Method(A() or B())
be invoked.

What you're describing here is precisely a kind of thing AOP (aspect-
oriented programming) provides. I suggest you read a bit about it in
general, say, on Wikipedia, and then have a look at PostSharp, which
is probably the most well-known AOP implementation for .NET:

http://www.postsharp.org/
 
Ad

Advertisements

What you're describing here is precisely a kind of thing AOP (aspect-
oriented programming) provides. I suggest you read a bit about it in
general, say, on Wikipedia, and then have a look at PostSharp, which
is probably the most well-known AOP implementation for .NET:

http://www.postsharp.org/- -

- -

It's great, Pavel ! thank you very much!

best regard,
Ares
 

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

Top