StaticLambda.Fody 1.0.2

There is a newer version of this package available.
See the version list below for details.
dotnet add package StaticLambda.Fody --version 1.0.2
                    
NuGet\Install-Package StaticLambda.Fody -Version 1.0.2
                    
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="StaticLambda.Fody" Version="1.0.2" />
                    
For projects that support PackageReference, copy this XML node into the project file to reference the package.
<PackageVersion Include="StaticLambda.Fody" Version="1.0.2" />
                    
Directory.Packages.props
<PackageReference Include="StaticLambda.Fody" />
                    
Project file
For projects that support Central Package Management (CPM), copy this XML node into the solution Directory.Packages.props file to version the package.
paket add StaticLambda.Fody --version 1.0.2
                    
#r "nuget: StaticLambda.Fody, 1.0.2"
                    
#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.
#:package StaticLambda.Fody@1.0.2
                    
#:package directive can be used in C# file-based apps starting in .NET 10 preview 4. Copy this into a .cs file before any lines of code to reference the package.
#addin nuget:?package=StaticLambda.Fody&version=1.0.2
                    
Install as a Cake Addin
#tool nuget:?package=StaticLambda.Fody&version=1.0.2
                    
Install as a Cake Tool

StaticLambda.Fody

NuGet package License

This is an add-in for Fody which turns lambda methods static if possible.

This project has a dependency to Emik.Morsels, if you are building this project, refer to its README first.



Why

This weaver exists because there is no way to guarantee that a lambda function will compile as a static method, even if you use the static keyword.

"No guarantee is made as to whether a static anonymous function definition is emitted as a static method in metadata. This is left up to the compiler implementation to optimize." - source

The reason is because delegates by nature are at their fastest invoking instanced methods, however in some cases it may be useful to make these methods static, mainly for reflection and interop purposes.

Of course, you can force it to be static by defining your own method to be static:

Foo(ThisInsteadOfALambda);

static void ThisInsteadOfALambda() { }

However, this may be an unsatisfactory solution as it creates far more verbose code, particularly when the parameter types are complicated and you use an API that has them inferred.

Installing this weaver will turn every applicable lambda into a static method. You do not need to specify static for this to take effect. Runtime performance is very likely barely worse with this weaver, but chances are the reflection that facillitates the need for static methods already makes this difference negligible.

Installation

  • Install the NuGet packages Fody and StaticLambda.Fody. Installing Fody explicitly is needed to enable weaving.

    PM> Install-Package Fody
    PM> Install-Package StaticLambda.Fody
    
  • Add the PrivateAssets="all" metadata attribute to the <PackageReference /> items of Fody and StaticLambda.Fody in your project file, so they won't be listed as dependencies.

  • If you already have a FodyWeavers.xml file in the root directory of your project, add the <StaticLambda /> tag there. This file will be created on the first build if it doesn't exist:

<Weavers>
    <StaticLambda />
</Weavers>

See Fody usage for general guidelines, and Fody Configuration for additional options.

Configuration

Define the compiler constant NO_STATIC_LAMBDA_FODY to skip this weaver. Useful to be passed as a command-line argument: e.g. dotnet build -define NO_STATIC_LAMBDA_FODY.

There is currently no way to define which classes or methods are affected. If you do require this level of granular control, please raise an issue, and preferably your use case.

Example

What you write:

Capture(() => 42);

static void Capture(System.Func<int> _) { }

What gets compiled:

// The Func<int> target normally is the <>c singleton (which is now redundant)
Capture(Program.<>c.<>9__0_0 ?? (Program.<>c.<>9__0_0 = new Func<int>(null, __methodptr(<Main>b__0_0))));

static void Capture(Func<int> _) { }

[CompilerGenerated, Serializable]
private sealed class <>c
{
    public static Func<int> <>9__0_0;

    // This method is normally non-static.
    internal static int <Main>b__0_0() => 42;
}

Contribute

Issues and pull requests are welcome to help this repository be the best it can be.

License

This repository falls under the MPL-2 license.

There are no supported framework assets in this package.

Learn more about Target Frameworks and .NET Standard.

This package has no dependencies.

NuGet packages

This package is not used by any NuGet packages.

GitHub repositories

This package is not used by any popular GitHub repositories.

Version Downloads Last Updated
1.1.0 129 12/10/2025
1.0.2 180 12/3/2025
1.0.1 176 12/3/2025
1.0.0 403 11/30/2025