Skip to content

RegisterAsAttribute TInterface

NexusLabs.Needlr

NexusLabs.Needlr

RegisterAsAttribute<TInterface> Class

Specifies that the decorated class should only be registered as the specified interface type, rather than all interfaces it implements.

public sealed class RegisterAsAttribute<TInterface> : System.Attribute
    where TInterface : class

Type parameters

TInterface

The interface type to register as. Must be an interface implemented by the decorated class.

Inheritance System.Object 🡒 System.Attribute 🡒 RegisterAsAttribute\<TInterface>

Example

public interface IReader { string Read(); }
public interface IWriter { void Write(string data); }
public interface ILogger { void Log(string message); }

// Only registered as IReader - not as IWriter or ILogger
[RegisterAs<IReader>]
public class FileService : IReader, IWriter, ILogger
{
    public string Read() => "data";
    public void Write(string data) { }
    public void Log(string message) { }
}

// Register as multiple specific interfaces
[RegisterAs<IReader>]
[RegisterAs<IWriter>]
public class DualService : IReader, IWriter, ILogger
{
    // Registered as IReader and IWriter, but NOT as ILogger
}

Remarks

By default, Needlr registers a class as all non-system interfaces it implements. Use this attribute when you want explicit control over which interface(s) are publicly resolvable from the container.

When this attribute is present, the class will ONLY be registered as the specified interface(s) and as itself (the concrete type). Other implemented interfaces will not be registered.

Multiple [RegisterAs] attributes can be applied to register the class as multiple specific interfaces while still excluding others.

Properties

RegisterAsAttribute<TInterface>.InterfaceType Property

Gets the interface type that the class should be registered as.

public System.Type InterfaceType { get; }

Property Value

System.Type