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)$B!#(BIs 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
 
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/
 
³

³ÂÏ£ÕÂ

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