Monday, March 17, 2008

Using Selective Method Weaving for LinFu.AOP

A while back, someone asked me if there was a way to make LinFu.AOP weave only certain types of methods and properties, and at the time, LinFu.AOP wasn't capable of doing that--that is, until now.

Here's how you do it. All you need to do is provide your own custom implementation of an interface named IMethodFilter and put that implementation in the same directory as LinFu.Aop.Weavers.Cecil.dll, and you're good to go. Here's a simple example:

[Implements(typeof(IMethodFilter), LifecycleType.OncePerRequest)]
public class MySampleCustomFilter : IMethodFilter
{
public bool ShouldWeave(MethodDefinition method)
{
// Intercept only public methods
return method.IsPublic;
}

}

All you have to do is put that in a separate DLL and make sure that the assembly is located in the same directory as LinFu.Aop.Weavers.dll, and you can customize it to your heart's content. It's just that simple. Enjoy!

2 comments:

  1. Does this mean that the same criteria will be used to decide if hooks will be injected for all woven assemblies? Can this be done on a per-project basis?

    Although, with the mechanism you've provided, it wouldn't be tough to create an assembly to read in a configuration file specifying per-project type and member elections.

    Keep up the great work.

    ReplyDelete
  2. Hi Dan,

    Selective Method Weaving allows you to tell the postweaver which methods you want to instrument or intercept, so it's up to you to decide how you want to weave your assemblies.

    As you can see, it's pretty simple to provide your own method filters, so if you want to do this on a per-project basis and do it through a config file, you shouldn't have any problems in doing so.

    The only thing that you have to watch out for is the distinction between intercepted types and woven types since the two sets of types won't always overlap. Other then that, you pretty much have everything you need to do whatever you need to do.

    ReplyDelete

Ratings by outbrain