CompileTimeWeaver.Fody 3.1.7

There is a newer version of this package available.
See the version list below for details.
dotnet add package CompileTimeWeaver.Fody --version 3.1.7
NuGet\Install-Package CompileTimeWeaver.Fody -Version 3.1.7
This command is intended to be used within the Package Manager Console in Visual Studio, as it uses the NuGet module's version of Install-Package.
<PackageReference Include="CompileTimeWeaver.Fody" Version="3.1.7">
  <PrivateAssets>all</PrivateAssets>
  <IncludeAssets>runtime; build; native; contentfiles; analyzers</IncludeAssets>
</PackageReference>
For projects that support PackageReference, copy this XML node into the project file to reference the package.
paket add CompileTimeWeaver.Fody --version 3.1.7
#r "nuget: CompileTimeWeaver.Fody, 3.1.7"
#r directive can be used in F# Interactive and Polyglot Notebooks. Copy this into the interactive tool or source code of the script to reference the package.
// Install CompileTimeWeaver.Fody as a Cake Addin
#addin nuget:?package=CompileTimeWeaver.Fody&version=3.1.7

// Install CompileTimeWeaver.Fody as a Cake Tool
#tool nuget:?package=CompileTimeWeaver.Fody&version=3.1.7

.Net Platforms

  • Framework 4.6.1
  • NetStandard 2.0

Compile time IL rewriting tool for AOP implementation

Different from runtime interception with dynamic proxy, this tool rewrites assembly at VisualStudio.Net build time, so that your code get these achievements that dynamic proxy based enhancement cannot give:

  • Much better performance, no dynamic proxy, no thread block or reflection at run time.
  • Directly instantiate your weaved classes with new operator
  • Weave virtual methods/properties
  • Weave static methods/properties
  • Weave extension methods
  • Weave constructors

Advice Based Programming Model

Start with version 2, this tool support advice based programming model. Advice based programming model has these superiorities comparing to the old decorator based programming model in version 1:

  • Easy to intercept async method without thread blocking
  • Simple to control the flow and add "before", "after", "around" and "exception" advises
  • Allow to advise methods with parameters of reference type, value type and generic type, ref parameters and out parameters are allowed, too.

Your Code

public class MyAdvice : AdviceAttribute
{
    public override object Advise(IInvocation invocation)
    {
        // do something before target method is Called
        // ...
        Trace.WriteLine("Entering " + invocation.Method.Name);

        try
        {
            return invocation.Proceed();	// call the next advice in the "chain" of advice pipeline, or call target method
        }
        catch (Exception e)
        {
            // do something when target method throws exception
            // ...
            Trace.WriteLine("MyAdvice catches an exception: " + e.Message);
            throw;
        }
        finally
        {
            // do something after target method is Called
            // ...
            Trace.WriteLine("Leaving " + invocation.Method.Name);
        }
    }

    public override async Task<object> AdviseAsync(IInvocation invocation)
    {
        // do something before target method is Called
        // ...
        Trace.WriteLine("Entering async " + invocation.Method.Name);

        try
        {
            return await invocation.ProceedAsync().ConfigureAwait(false);	// asynchronously call the next advice in advice pipeline, or call target method
        }
        catch (Exception e)
        {
            // do something when target method throws exception
            // ...
            Trace.WriteLine("MyAdvice catches an exception: " + e.Message);
            throw;
        }
        finally
        {
            // do something after target method is Called
            // ...
            Trace.WriteLine("Leaving async " + invocation.Method.Name);
        }
    }
}

[MyAdvice]
public class MyClass
{
	public int Add(int x, int y)
	{
		return x + y;
	}

	public Task<int> AddAsync(int x, int y)
	{
		await Task.Dely(1000).ConfigureAwait(false);
		return x + y;
	}
}

The first time when you compile MyClass class, FodyWeavers.xml and FodyWeavers.xsd are generated if they do not exist yet. FodyWeavers.xml content likes this below:

<?xml version="1.0" encoding="utf-8"?>
<Weavers xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="FodyWeavers.xsd">
  <CompileTimeWeaver />
</Weavers>

If (1) your MyClass and MyAdvice are in different assemblies and (2) your target platform is netstandard, you need to manually add the full name of MyAdvice class into AdviceAttributes property of CompileTimeWeaver section in FodyWeavers.xml file as below (suppose the namespace of MyAdvice is Advices here):

  <CompileTimeWeaver AdviceAttributes="Advices.MyAdvice" />

To add more advice classes into AdviceAttributes property, seperate them with '|' delimitor, for example: AdviceAttributes="Advices.MyAdvice1|Advices.MyAdvice2".

You do not have to change this file if your target is .net Framework or MyClass and MyAdvice are in same assembly.

Notes:

  • When multiple advices are applied to a method, the advices are invoked as a pipe line, the Advise() or AdviseAsync() of the first advice is called first.
  • When an advice is applied to class level, it is identical to this advice is added on each constructor and each method.
  • Class level advices appears before all constructor/method level advices
  • Each advice is assigned an Order, it is the sequence number of the appearance in the type group.
  • Each advice on a recursive method is invoked only when the method was entered at the very first time, re-entrances don't invoke the advice again, for performance reason.
Product Compatible and additional computed target framework versions.
.NET net5.0 was computed.  net5.0-windows was computed.  net6.0 was computed.  net6.0-android was computed.  net6.0-ios was computed.  net6.0-maccatalyst was computed.  net6.0-macos was computed.  net6.0-tvos was computed.  net6.0-windows was computed.  net7.0 was computed.  net7.0-android was computed.  net7.0-ios was computed.  net7.0-maccatalyst was computed.  net7.0-macos was computed.  net7.0-tvos was computed.  net7.0-windows was computed.  net8.0 was computed.  net8.0-android was computed.  net8.0-browser was computed.  net8.0-ios was computed.  net8.0-maccatalyst was computed.  net8.0-macos was computed.  net8.0-tvos was computed.  net8.0-windows was computed. 
.NET Core netcoreapp2.0 was computed.  netcoreapp2.1 was computed.  netcoreapp2.2 was computed.  netcoreapp3.0 was computed.  netcoreapp3.1 was computed. 
.NET Standard netstandard2.0 is compatible.  netstandard2.1 was computed. 
.NET Framework net461 is compatible.  net462 was computed.  net463 was computed.  net47 was computed.  net471 was computed.  net472 was computed.  net48 was computed.  net481 was computed. 
MonoAndroid monoandroid was computed. 
MonoMac monomac was computed. 
MonoTouch monotouch was computed. 
Tizen tizen40 was computed.  tizen60 was computed. 
Xamarin.iOS xamarinios was computed. 
Xamarin.Mac xamarinmac was computed. 
Xamarin.TVOS xamarintvos was computed. 
Xamarin.WatchOS xamarinwatchos was computed. 
Compatible target framework(s)
Included target framework(s) (in package)
Learn more about Target Frameworks and .NET Standard.
  • .NETFramework 4.6.1

  • .NETStandard 2.0

NuGet packages (1)

Showing the top 1 NuGet packages that depend on CompileTimeWeaver.Fody:

Package Downloads
CompileTimeAOP.TransactionAwareness

A VisualStudio tool to allow programmers to develop Transaction Aware classes for NHibernate. TransactionAware AOP is implemented at compile time with CompileTimeWeaver.Fody.

GitHub repositories

This package is not used by any popular GitHub repositories.

Version Downloads Last updated
3.3.5 86 4/1/2024
3.3.4 3,571 2/1/2023
3.3.3 761 1/1/2022
3.1.7 1,617 2/6/2019
2.3.0 23,569 5/2/2017
2.2.1 1,393 4/30/2017
2.1.1 1,458 4/9/2017
2.1.0 1,005 4/9/2017
2.0.1 1,061 3/26/2017
2.0.0 1,003 3/24/2017
1.4.3 1,461 3/11/2017
1.4.2 1,464 3/8/2017
1.4.1 1,539 3/8/2017
1.3.0 1,426 2/27/2017
1.1.0 1,263 2/23/2017
1.0.0 1,046 2/23/2017

- Support Netstandard 2.0 and Framework 4.6.1
     - Abandoned support to Decorator Based Programming Model