InjectionExpert 0.2.1

dotnet add package InjectionExpert --version 0.2.1
                    
NuGet\Install-Package InjectionExpert -Version 0.2.1
                    
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="InjectionExpert" Version="0.2.1" />
                    
For projects that support PackageReference, copy this XML node into the project file to reference the package.
<PackageVersion Include="InjectionExpert" Version="0.2.1" />
                    
Directory.Packages.props
<PackageReference Include="InjectionExpert" />
                    
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 InjectionExpert --version 0.2.1
                    
#r "nuget: InjectionExpert, 0.2.1"
                    
#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 InjectionExpert@0.2.1
                    
#: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=InjectionExpert&version=0.2.1
                    
Install as a Cake Addin
#tool nuget:?package=InjectionExpert&version=0.2.1
                    
Install as a Cake Tool

Injection Expert

Injection Expert is a dependency injection framework for .NET, featuring dynamic code generation for high performance and flexibility. It supports constructor and member injection, generic type resolution, and advanced configuration options.

Compared to Microsoft.Extensions.DependencyInjection, this library offers more flexibility by allowing dynamically adding injections. This library has good compatibility with Microsoft.Extensions.DependencyInjection, IInjectionProvider of this library can be easily adapted to IServiceProvider, and vice versa.

Key Concepts

  • InjectionContainer: The registry for dependencies. Supports singleton and transient lifetimes, generic type mapping, and resolution of injections.
  • ConstructorInjector: Dynamically generates code to inject dependencies via constructors.
  • MemberInjector: Dynamically generates code to inject dependencies into fields and properties marked with [Injection] attribute.
  • Attributes: [Injection] requires injector to inject fields/properties without required keyword, or ignore required members. This attribute can also indicate whether the member needs keyed injection.

Rules

Rules for Selecting Members to Inject

Fields or properties must fulfill all requirements at the same time:

  • Is marked with required keyword or [Injection] attribute when enabled parameter is true.

  • Is not a literal or init-only field.

  • Is not a read-only property.

  • Rules for Selecting Constructors to Inject

  1. Constructors with the lowest number of parameters are prioritized.
  2. The first construct that all parameters can be resolved is selected.

Usage

Registering and Resolving Dependencies

var container = new InjectionContainer()
    .AddSingleton(1)
    .AddSingleton(0.5)
    .AddSingleton("Sample");

// Constructor injection
var succeeded = ConstructorInjector
    .For(typeof(MyClass))
    .TryInject(out var instance, container, out _);

var target = (MyClass?)instance;
if (succeeded && target != null)
{
    Console.WriteLine(target.Text);   // "Sample"
    Console.WriteLine(target.Number); // 1
    Console.WriteLine(target.Value);  // 0.5
}

Member Injection

public class MyTarget
{
    // This field will be injected because it is marked with [Injection]
    [Injection] public int NumberField = 0;
    // This field will be injected because it is marked as required
    public required string StringField = "";
    // This field will NOT be injected even it is a required member, because it is marked with [Injection(enabled: false)
    [Injection(false)] public required int IgnoredMember = "";
}

var container = new InjectionContainer()
    .AddSingleton(1)
    .AddSingleton("Sample");

var sample = new MyTarget();
MemberInjector
    .For(typeof(MyTarget))
    .TryInject(sample, container, out _);

// sample.NumberField == 1
// sample.StringField == "Sample"

Generic Type Registration

container.AddTransient(typeof(IMyGenericInterface<,>), typeof(MyGenericType<,>));
var instance = container.GetInjection<IMyGenericInterface<int, long>>();
// instance is resolved as MyGenericType<long, int>

Features

  • High-performance dynamic code generation for injection.
  • Supports both constructor and member injection.
  • Generic type mapping and resolution.
  • Attribute-based configuration for fine control.
  • Extensible and suitable for advanced scenarios.

For advanced usage and configuration, please refer to the source code and test cases.

Product Compatible and additional computed target framework versions.
.NET net9.0 is compatible.  net9.0-android was computed.  net9.0-browser was computed.  net9.0-ios was computed.  net9.0-maccatalyst was computed.  net9.0-macos was computed.  net9.0-tvos was computed.  net9.0-windows was computed.  net10.0 was computed.  net10.0-android was computed.  net10.0-browser was computed.  net10.0-ios was computed.  net10.0-maccatalyst was computed.  net10.0-macos was computed.  net10.0-tvos was computed.  net10.0-windows was computed. 
Compatible target framework(s)
Included target framework(s) (in package)
Learn more about Target Frameworks and .NET Standard.

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
0.2.1 111 9/2/2025
0.2.0 102 9/2/2025
0.1.1 231 8/25/2025
0.1.0 91 8/17/2025

Version 0.2.1

Breaking Changes:
- Add a struct 'InjectionItem' to replace the previous '(object, InjectionLifespan)'.
- Separate the functor support from 'IInjectionContainer' to 'FunctorInjectionProvider';
 now functor wrapper can cache singleton instances;
 users can extend an injection provider by combining functor wrapper and chained wrapper.

Improvements:
- Unify the word usage in documentation.