Automatic code injection/invocation. Does it exist?

  • Thread starter Thread starter bobben
  • Start date Start date
B

bobben

Hi everybody.

I don't know if this is possible, but I am looking for a way to
automatically inject code.

I would like to fire a Debug.Writeline() automatically when a method is
invoked and when the method exists.

Anybody knows if this is at all possible? Or is any other variant possible ?
Is custom attributes a way to go?

Basically I want a similar behaviour as ServicedComponent class with its
[Autocomplete] attribute:
It does one thing if all goes well and another thing if exceptions occurs?

Any ideas anybody...

thank anyway!


Bobben
 
Bobben,

Basically, you are looking for something like Aspect Oriented
Programming. While AOP offers you other ways to weave aspects, the basic
mechanism that you are looking for is method interception. .NET does offer
something like this, using Context-bound objects. However, there is a price
to pay for it.

A good article to read on this is on MSDN, titled "Decouple Components
by Injecting Custom Services into Your Object's Interception Chain", located
at (watch for line wrap):

http://msdn.microsoft.com/msdnmag/issues/03/03/ContextsinNET/default.aspx

It will show you how to do exactly what you want.

Hope this helps.
 
"Nicholas Paldino [.NET/C# MVP]" <[email protected]> wrote
in message news:[email protected]...
Basically, you are looking for something like Aspect Oriented
Programming. While AOP offers you other ways to weave aspects, the basic
mechanism that you are looking for is method interception. .NET does offer
something like this, using Context-bound objects. >

Yep. Something like, but more general, called Program Transformations,
which replace specified source patterns with others source patterns.
In Bobben's case, he wants to replace the method head with the
same method head and added instrumentation. See
http://www.semanticdesigns.com/Products/DMS/ProgramTransformations
for more information on this.

See http://www.semanticdesigns.com/Products/TestCoverage for a whitepaper
showing exactly how to use program transformations to insert instrumentation
probes just like this.
 
Back
Top