vescon / MethodBoundaryAspect.Fody

A Fody weaver which allows to decorate methods and hook into method start, method end and method exceptions.
MIT License
243 stars 75 forks source link

Can MethodBoundaryAspect.Fody be used to apply aspects to third-party NuGet packages without modifying their source code? #117

Open InCerryGit opened 1 year ago

InCerryGit commented 1 year ago

Hi,

I have a project A that depends on a third-party NuGet package B. I cannot modify the source code of package B. There's a method called MethodA inside package B, and I would like to apply AOP aspects using MethodBoundaryAspect.Fody to this method without altering the project A.

Is it possible to achieve this using MethodBoundaryAspect.Fody? If so, could you please provide some guidance or examples on how to implement this?

Thank you.

Ralf1108 commented 1 year ago

Hi,

through the built-in mechanism of Fody it is not possible. Fody runs as an additional build step after compilation so you would need the source code to use it. First source code is compiled to an assembly and then Fody invokes the configured weavers and provide the assembly with it.

So the weaving logic won't care if the assembly is fresh compiled or if it already existed before. The difference is that you have to start the weaving process via you own way.

As Fody is using "Mono.Cecil" under the hood please google it. It is just a .Net library for parsing and modifying assemblies. When get it running you can just invoke the entrance weaver class by yourself with the appropriate properties set.

As your assembly is already compiled how will you mark the methods which should be weaved? Normaly this is done via adding .Net attributes in the source code. But you can change method discovery mechanism in the weaver code also, e.g. hard code method names/namespaces.

Please keep us updated and maybe show your solution. Maybe others are also interested in this and it leads to an own project :-)

Ralf1108 commented 1 year ago

Place where methods to weave are selected