Skip to content

KeyedAttribute

NexusLabs.Needlr

NexusLabs.Needlr

KeyedAttribute Class

Marks a class for keyed service registration. The type will be registered with the specified service key in addition to its normal registration.

public sealed class KeyedAttribute : System.Attribute

Inheritance System.Object 🡒 System.Attribute 🡒 KeyedAttribute

Remarks

Use this attribute when you want to register multiple implementations of the same interface with different keys, allowing consumers to resolve specific implementations using [FromKeyedServices("key")].

Example:

[Keyed("stripe")]
public class StripeProcessor : IPaymentProcessor { }

[Keyed("paypal")]
public class PayPalProcessor : IPaymentProcessor { }

// Consumer resolves specific implementation:
public class OrderService(
    [FromKeyedServices("stripe")] IPaymentProcessor processor) { }

Constructors

KeyedAttribute(string) Constructor

Initializes a new instance of the KeyedAttribute class.

public KeyedAttribute(string key);

Parameters

key System.String

The service key for this registration.

Properties

KeyedAttribute.Key Property

Gets the service key for this registration.

public string Key { get; }

Property Value

System.String