How to do something automatic before call a method?

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • =?GB2312?B?s8LPo9XC?=

    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(){
    DoSomethingFirs t();
    //continue to do method logic
    }
    public void B(){
    DoSomethingFirs t();
    //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
  • Pavel Minaev

    #2
    Re: How to do something automatic before call a method?

    On Aug 2, 11:30 am, ³ÂÏ£Õ <cxizh...@gmail .comwrote:
    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(){
    DoSomethingFirs t();
    //continue to do method logic}
    >
    public void B(){
    DoSomethingFirs t();
    //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:



    Comment

    • =?GB2312?B?s8LPo9XC?=

      #3
      Re: How to do something automatic before call a method?

      On 8ÔÂ2ÈÕ, ÏÂÎç4ʱ18·Ö, Pavel Minaev <int...@gmail.c omwrote:
      On Aug 2, 11:30 am, ³ÂÏ£Õ <cxizh...@gmail .comwrote:
      >
      >
      >
      >
      >
      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(){
      DoSomethingFirs t();
      //continue to do method logic}
      >
      public void B(){
      DoSomethingFirs t();
      //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/- Òþ²Ø±»ÒýÓÃÎÄ×Ö -
      >
      - ÏÔʾÒýÓõÄÎÄ×Ö -
      It's great, Pavel ! thank you very much!

      best regard,
      Ares

      Comment

      Working...