Autofac.Extras.AttributeMetadata
August 14, 2026 ยท View on GitHub
Attribute metadata support for Autofac IoC
Please file issues and pull requests for this package in this repository rather than in the Autofac core repo.
Quick Start
First, create a metadata attribute - any System.Attribute that has MEF's [MetadataAttribute] applied. Every publicly readable property becomes one metadata name/value pair, so the attribute below provides Age metadata:
[MetadataAttribute]
public class AgeMetadataAttribute : Attribute
{
public int Age { get; private set; }
public AgeMetadataAttribute(int age)
{
Age = age;
}
}
Apply it to the component implementation rather than to the service interface:
public interface IArtwork
{
void Display();
}
[AgeMetadata(100)]
public class CenturyArtwork : IArtwork
{
public void Display() { ... }
}
Then register the AttributedMetadataModule so the container reads those attributes, and consume the metadata as you would any other:
var builder = new ContainerBuilder();
builder.RegisterModule<AttributedMetadataModule>();
builder.RegisterType<CenturyArtwork>().As<IArtwork>();
var container = builder.Build();
var artwork = container.Resolve<IEnumerable<Meta<IArtwork>>>()
.First(a => a.Metadata["Age"].Equals(100));
artwork.Value.Display();
You can read more details in the documentation.
Get Help
Need help with Autofac? We have a documentation site as well as API documentation. We're ready to answer your questions on Stack Overflow or check out the discussion forum.