0

Is there an ActionFilter equivalent for methods in c# itself?

In controllers you can have a custom filter which does a task OnActionExecuted and OnActionExecuting but i'd like to do a similar thing on methods inside a class as well.

So i'd like to do:

[MyMethodFilter]
public static string Convert(string source)
{
     //DO STUFF
     //RETURN A STRING
}

The IActionFilter interface is part of System.Web.Mvc

tereško
  • 58,060
  • 25
  • 98
  • 150
Sam Jones
  • 4,443
  • 2
  • 40
  • 45

1 Answers1

0

You are looking for aspect-oriented programming. There are some ways to do it in C#:

  1. Proxy-class derived from your class. It is easy to do with DI-container: How to set up an optional method interception with Ninject?

  2. MSIL Rewriting. Much more complicated technology. PostSharp is a good library to start with it: http://doc.postsharp.net/method-interception

So, there is no a ready-to-use ActionFilter equivalent. Howewer you can achieve similar functionality.

Community
  • 1
  • 1
Ilya Chumakov
  • 23,161
  • 9
  • 86
  • 114