473,394 Members | 1,746 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,394 software developers and data experts.

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
Aug 2 '08 #1
2 3022
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(){
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/

Aug 2 '08 #2
On 8ÔÂ2ÈÕ, ÏÂÎç4ʱ18·Ö, Pavel Minaev <int...@gmail.comwrote:
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(){
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/- Òþ²Ø±»ÒýÓÃÎÄ×Ö -

- ÏÔʾÒýÓõÄÎÄ×Ö -
It's great, Pavel ! thank you very much!

best regard,
Ares
Aug 2 '08 #3

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

9
by: Paul Rubin | last post by:
I'm trying the super() function as described in Python Cookbook, 1st ed, p. 172 (Recipe 5.4). class A(object): def f(self): print 'A' class B(object): def f(self):
7
by: Ben R. | last post by:
How does automatic type casting happen in vb.net? I notice that databinder.eval "uses reflectoin" to find out the type it's dealing with. Does vb.net do the same thing behind the scenes when an...
59
by: MotoK | last post by:
Hi Experts, I've just joined this group and want to know something: Is there something similar to smart pointers in C or something to prevent memory leakages in C programs. Regards MotoK
20
by: James | last post by:
I am new to vb.net (I am an old vb6 programmer). I cannot figure out an easy way to watch variables. In vb6 all I did was right click and could chose to stop execution when a variable chances as...
11
by: matsi.inc | last post by:
I am looking to make something like a delegate that i can use in my projects but am having a hard time getting started. The behavior I am most interested in is how a delegate changes it's Invoke...
4
by: Kevin Frey | last post by:
I have an assembly written in C++/CLI that also links in non-clr Native C++ (the C++/CLI wraps the Native C++ functionality). This assembly has an in-built tracing system that needs to be...
3
by: Fei Liu | last post by:
Hello, We all know that a template function can automatically deduce its parameter type and instantiate, e.g. template <tpyename T> void func(T a); func(0.f); This will cause func<floatto...
0
by: RG | last post by:
I have a stored procedure which is called by db2 "call ....." statement from ksh script. When running from AIX DB2 v. 9.1.3 environment, after a while the stored procedure starts to hang and...
4
by: Joerg Battermann | last post by:
Hello there, does anyone know the precise naming conventions used for internal backing fields for automatic properties? Something official besides looking at the compiled assemblies that might...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.