AW.NamedTypes 1.0.0

dotnet add package AW.NamedTypes --version 1.0.0                
NuGet\Install-Package AW.NamedTypes -Version 1.0.0                
This command is intended to be used within the Package Manager Console in Visual Studio, as it uses the NuGet module's version of Install-Package.
<PackageReference Include="AW.NamedTypes" Version="1.0.0" />                
For projects that support PackageReference, copy this XML node into the project file to reference the package.
paket add AW.NamedTypes --version 1.0.0                
#r "nuget: AW.NamedTypes, 1.0.0"                
#r directive can be used in F# Interactive and Polyglot Notebooks. Copy this into the interactive tool or source code of the script to reference the package.
// Install AW.NamedTypes as a Cake Addin
#addin nuget:?package=AW.NamedTypes&version=1.0.0

// Install AW.NamedTypes as a Cake Tool
#tool nuget:?package=AW.NamedTypes&version=1.0.0                

AW.NamedTypes

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.

Features

  • 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.

Installation

You can add this library to your project via NuGet

Usage

Basic Registration

You can manually register a type with a custom name:

using AW.NamedTypes;

public record OrderCreatedEvent(Urn OrderId, ...);

...

TypeRegistry.Instance.Register(typeof(MyCustomClass), "order-created-event");

...

Type Resolution

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}");
}

...

Auto-Registering Types with [NamedType] Attribute

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 AutoRegisterTypes method can also be called without parameters to scan all loaded assemblies.

License

This project is licensed under the MIT License.

Product Compatible and additional computed target framework versions.
.NET net8.0 is compatible.  net8.0-android was computed.  net8.0-browser was computed.  net8.0-ios was computed.  net8.0-maccatalyst was computed.  net8.0-macos was computed.  net8.0-tvos was computed.  net8.0-windows was computed. 
Compatible target framework(s)
Included target framework(s) (in package)
Learn more about Target Frameworks and .NET Standard.
  • net8.0

    • No dependencies.

NuGet packages

This package is not used by any NuGet packages.

GitHub repositories

This package is not used by any popular GitHub repositories.

Version Downloads Last updated
1.0.0 88 11/3/2024