1

I'd like to create a new custom "[Tag]" with a particular semantic purpose.

Image you have a list of method in a service and you want some of them to Log some information: actually I'm doing something like

public void Method(parameters)
{
   LogMethodCall();

   //Method Body
}

Actually I'd prefere to create something like:

[LogEnabled]
public void Method(parameters)
{
   //Method Body
}

And have somewhere stored something like:

if (Method is LogEnabled) LogMethodCall()

Is this possible? Exist this possibility in C#? How is it called?

Micha
  • 5,117
  • 8
  • 34
  • 47
Ziba Leah
  • 2,484
  • 7
  • 41
  • 60
  • Never tested it, but you might want to have a look at: http://www.postsharp.net/ – ken2k Feb 17 '14 at 10:18
  • possible duplicate of [Aspect Oriented Programming in C#](http://stackoverflow.com/questions/1416880/aspect-oriented-programming-in-c-sharp) – Dennis Feb 17 '14 at 10:21
  • @Dennis How is that a duplicate? – svick Feb 17 '14 at 11:26
  • @svick, the answer leads the user to leverage PostSharp. This is *exactly* what they do. – Mike Perrenoud Feb 27 '14 at 20:38
  • @MichaelPerrenoud But that doesn't make it a duplicate, the two questions are completely different. Or are you saying that all AOP-related questions should be closed as a duplicate of that question (which is now actually closed)? – svick Feb 27 '14 at 21:23
  • @svick, if the solution is exactly the same, how is it not a duplicate? And i'm asking without sarcasm. – Mike Perrenoud Feb 27 '14 at 21:25
  • 1
    @MichaelPerrenoud Except the solution isn't exactly the same. I don't see anything similar to the code from Adassko's answer in that possible duplicate. And even then, I think questions shouldn't be closed as duplicated based on the *answers*. Only the questions themselves should matter. – svick Feb 27 '14 at 21:31

1 Answers1

2

Use postsharp:

With it you can create something that is called an aspect. Yours will look like:

public class LogEnabled : OnMethodBoundaryAspect
{
        public override void OnEntry(MethodExecutionArgs args)
        {
            Logger.LogMethodCall();
        }
}

Free, express edition will be all you need

And those are called attributes, not "tags"

Adassko
  • 5,201
  • 20
  • 37