InjectionExpert 0.3.0-dev11152241

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.

Showing the top 20 packages that depend on InjectionExpert.

Packages Downloads
SnapshotExpert
A library for automatic BSON/JSON snapshot serailization, with built-in support for reference tracking.
5
SnapshotExpert
A library for automatic BSON/JSON snapshot serailization, with built-in support for reference tracking.
4
SnapshotExpert
A library for automatic BSON/JSON snapshot serailization, with built-in support for reference tracking.
2
SnapshotExpert
A library for automatic BSON/JSON snapshot serailization, with built-in support for reference tracking.
1
SnapshotExpert
A library for automatic BSON/JSON snapshot serailization, with built-in support for reference tracking.
0

Version Downloads Last updated
0.3.0-dev11212052 3 11/21/2025
0.3.0-dev11212051 1 11/21/2025
0.3.0-dev11202222 0 11/20/2025
0.3.0-dev11192209 2 11/19/2025
0.3.0-dev11192128 1 11/19/2025
0.3.0-dev11192121 1 11/19/2025
0.3.0-dev11192110 1 11/19/2025
0.3.0-dev11192033 0 11/19/2025
0.3.0-dev11182023 2 11/18/2025
0.3.0-dev11160049 3 11/15/2025
0.3.0-dev11152351 1 11/15/2025
0.3.0-dev11152244 1 11/15/2025
0.3.0-dev11152241 1 11/15/2025
0.3.0-dev11152200 1 11/15/2025
0.3.0-dev11141721 2 11/14/2025
0.3.0-dev10301930 2 10/30/2025
0.3.0-dev10301925 1 10/30/2025
0.3.0-dev10301709 0 10/30/2025
0.3.0-dev10292034 0 10/29/2025
0.3.0-dev10281837 1 10/28/2025
0.3.0-dev10232052 0 10/28/2025
0.3.0-dev10232040 1 10/23/2025
0.3.0-dev10232031 1 10/23/2025
0.3.0-dev10232002 1 10/23/2025
0.3.0-dev10231936 1 10/23/2025
0.3.0-dev10231715 1 10/23/2025
0.3.0-dev10212010 1 10/21/2025
0.3.0-dev10161638 0 10/16/2025
0.3.0-dev10161556 2 10/16/2025
0.3.0-dev09102118 5 09/10/2025
0.3.0-dev09092308 2 09/09/2025
0.3.0-dev09082141 2 09/08/2025
0.3.0-dev09070007 3 09/06/2025
0.2.3-dev09042041 3 09/04/2025
0.2.2 3 09/02/2025