How to write a custom attribute in .Net that do someting on runtime before/after a method?

How to write a custom attribute in .Net that will do someting on runtime before/after a method code?

for example:

implement a "Trace" attribute which write a line to file when the program is enter and when it exit from myMethod.

[Trace()]

void myMethod()

{

...

}


Would you like to answer or comment?

Sign up for a free account, or sign in (if you're already a member).
  • 916 views
Share Send to a friend Watch Report
 
 

Posted Answers

Order by
 

you can use a conditional attribute.

Marks a conditional method whose execution depends on a specified preprocessing identifier.

sample:

#define TRACE
using System;
using System.Diagnostics;
public class Trace
{
   [Conditional("TRACE")] public static void Msg(string msg)
   {
      Console.WriteLine(msg);
   }
}
class Test
{
   static void A( )
   {
      Trace.Msg("Now in A.");
      B( );
   }
   static void B( )
   {
      Trace.Msg("Now in B.");
   }
   public static void Main( )
   {
      Trace.Msg("Now in Main.");
      A( );
      Console.WriteLine("Done.");
   }
}
-------------------------

Output:
Now in Main.
Now in A.
Now in B.
Done.

If you compile the sample with first line omitted (or changed to #undef DEBUG), the output will be: Done


Posted 2 years ago ( permalink )
In reply to ymushkin's question
talh was invited by Yedda to answer this question.

Rated as
#5 out of 5
2
0

Helpful?

line
line
line



 
9 thumbs up

Thank, but this is not what I'm asking for.

I want to implement Trace attribute that each method that will decorate with this attribute will be traced in and trace out on runtime.

The way that I found is to use ContextBoundObject and ContextAttribute (For more details, see: http://msdn.microsoft.com/msdnmag/issues/02/03/AOP/default.aspx)

The main problems in this way, is that I'm enforced to inherit my class from ContextBoundObject and also there are performance issue using ContextBoundObject.

 I want it to be more elegant and decoupled. I want my code to be like the following:

Assembly A: 

class TraceAttribute: Attribute

{

}

 

Assembly B: 

class MyClass

{

    [Trace] 

    void f()

    {

    } 

 

The Trace attribute will cause some action when enter to the function f() and when exit from the function. (injection or any other way) 


Posted 2 years ago ( permalink )
In reply to talh's answer
Rated as
#2 out of 5
0
0

Helpful?

line
line
line



 
345 thumbs up

Rejected slogan #235:

Yedda - a brain the size of a planet.

You might be able to implement what you're looking for using RealProxy.

You still need to inherit your class from a specific class using this technique, but luckily, you need to inherit from MarshalByRefObject, which is a lot more light-weight then ContextBoundObject.

The following article in CodeProject should get you started:

Using Attributes and RealProxy to perform property level processing 

Let us know if you get it working, we all want to [trace] Smile


Posted 2 years ago ( permalink )
In reply to ymushkin's answer
Rated as
#3 out of 5
0
0

Helpful?

line
line
line



 

i think this article can help you

 Decouple Components by Injecting Custom Services into Your Object's Interception Chain

is professional term  call aspect

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


Posted 2 years ago ( permalink )
In reply to ymushkin's answer
talh was invited by Yedda to answer this question.

Rated as
#4 out of 5
0
0

Helpful?

line
line
line



 
12 thumbs up

"Society exists only as a mental concept; in the real world there are only individuals"

Read what matters to you, go Spotback!

Have a look in

http://www.castleproject.org/index.php/DynamicProxy, this is the code that enables aspect programming to be implemented in .NET. Basically it enables you to intercept calls to methods with very little change to your code (very similar to the dynamic proxy class first introduced in java).

Posted 2 years ago ( permalink )
In reply to ymushkin's question
Rated as
Best Answer
0
4

Helpful?

line
line
line