1

I'm trying to use fody with MethodDecorator on assembly level.

I tried to use the attribute provided by the sample code without any changes:

[AttributeUsage(AttributeTargets.Method | AttributeTargets.Constructor | 
AttributeTargets.Assembly | AttributeTargets.Module)]
public class InterceptorAttribute : Attribute, IMethodDecorator {
    // The code of the implemented methods doesn't matter.
}

If I decorate a method using [Interceptor] all is working as expected.

But I try to intercept every method in my assembly. The sample code targets this attribute to AttributeTargets.Assembly. That signals me that this might be working.

To get that done I added [assembly: InterceptorAttribute] to AssemblyInfo.cs of my project.

The code still compiles without any error but the interceptor will not be called.

The same happens if I use [module: Interceptor].

What do I have to do to get this working?
Is there any other way to intercept every method in an assembly/module?

UPDATE

I found MethodBoundaryAspect that works as expected.

Sebastian Schumann
  • 3,204
  • 19
  • 37
  • How did you end up having Fody automatically weave every method in the assembly without manually decorating each method with the attribute tag? – J.D. Jan 06 '21 at 21:58
  • 1
    @J.D. As far as I remember I didn't find a perfect solution. My use case was to ensure an `SynchronizationContext` for each UI interaction in an Excel-AddIn. The used solution was to wave with `MethodBoundaryAspect`, create an Attribute that ensures it for a class and decorate each class (mainly UI-Forms) that need that context. Decorating each needed class was the only solution that we found - but it was practicable. – Sebastian Schumann Jan 11 '21 at 05:53
  • Thanks! Unfortunately I haven't found a solution yet myself but would've thought nowadays Fody (or one of its extensions) would've had a simple way to weave every method and/or class automatically without having to apply attributes. Seems like that's not possible, that I've found so far at least. My end goal is to automatically log every method in a specific app and to streamline the process so developers don't forget to tag their methods accordingly as they build up this app. I'll have other use cases for injecting code into every method too so didn't want to use just a logging extension. – J.D. Jan 11 '21 at 12:49
  • @J.D. I haven't tested it but maybe a [Tracer](https://github.com/csnemes/tracer) is what you're looking for the logging aspect. Did you try `MethodBoundaryAspect`? The last commits look like they did anything to prevent weaving every method (for special cases) - so I thought there muste be a solution. – Sebastian Schumann Jan 13 '21 at 05:33
  • Thanks, when I looked into MethodBoundaryAspect, all the documentation implied I need to manually decorate my methods with an attribute (if you find doc or an example otherwise, I'd definitely be interested). I did see a couple Tracer extensions, one specifically that does allow you to specify "weave all methods" but it was dated, hardly used, and required a downgrade of the main Fody assembly to a version older than 4.0 lol (which would break other Fody dependent extensions I'm currently using). – J.D. Jan 13 '21 at 13:53

1 Answers1

0

You can do this by implanting the Attribute class from the IAspectMatchingRule interface. Like this;

[AttributeUsage( AttributeTargets.Method | AttributeTargets.Constructor | AttributeTargets.Class | AttributeTargets.Module | AttributeTargets.Assembly, AllowMultiple=true)] public class MatchingDecoratorAttribute : Attribute, IAspectMatchingRule, IMethodDecorator

The IAspectMatchingRule interface includes the AttributeTargetTypes property to allow filtering methods in the assembly. After you create the attribute this way, you can apply it in the AssemblyInfo.cs file as follows;

[assembly: MatchingDecorator(AttributeTargetTypes="SimpleTest.DerivedMatchingAssembly.*")]

Fody MethodDecorator Allow global application of method decorations by attribute #45