AW.NamedTypes is a .NET library for managing and dynamically resolving types by name. It provides a registry (TypeRegistry) that allows for registering types with custom names and resolving them back by name or type. This can be useful for plugins, dependency injection, and scenarios requiring dynamic type loading.
- Register types with a custom name.
- Resolve types by name or retrieve names from registered types.
- Automatically register types annotated with the
[NamedType]attribute from specified assemblies.
You can add this library to your project via NuGet
You can manually register a type with a custom name:
using AW.NamedTypes;
public record OrderCreatedEvent(Urn OrderId, ...);
...
TypeRegistry.Instance.Register(typeof(OrderCreatedEvent), "order-created-event");
...
You can resolve a type by its name or retrieve the registered name of a type:
using AW.NamedTypes;
...
if (TypeRegistry.Instance.TryResolveType("order-created-event", out Type? type))
{
Console.WriteLine($"Resolved Type: {type.FullName}");
}
if (TypeRegistry.Instance.TryResolveName(typeof(OrderCreatedEvent), out string? typeName))
{
Console.WriteLine($"Resolved Name: {typeName}");
}
...
To streamline type registration, you can annotate classes with the [NamedType] attribute and auto-register them from specific assemblies:
using AW.NamedTypes;
[NamedType("order-created-event")]
public record OrderCreatedEvent(Urn OrderId, ...);
[NamedType("order-cancelled-event")]
public record OrderCancelledEvent(Urn OrderId, ...);
...
// Register all types with NamedType attributes from the current assembly
TypeRegistry.Instance.AutoRegisterTypes(Assembly.GetExecutingAssembly());
...
This is useful when types need to be available at runtime for deserialization or dynamic loading but may not be directly referenced in the code.
The
AutoRegisterTypesmethod can also be called without parameters to scan all loaded assemblies.
This project is licensed under the MIT License.