ReflectionFactory 1.0.1

There is a newer version of this package available.
See the version list below for details.
dotnet add package ReflectionFactory --version 1.0.1                
NuGet\Install-Package ReflectionFactory -Version 1.0.1                
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="ReflectionFactory" Version="1.0.1" />                
For projects that support PackageReference, copy this XML node into the project file to reference the package.
paket add ReflectionFactory --version 1.0.1                
#r "nuget: ReflectionFactory, 1.0.1"                
#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 ReflectionFactory as a Cake Addin
#addin nuget:?package=ReflectionFactory&version=1.0.1

// Install ReflectionFactory as a Cake Tool
#tool nuget:?package=ReflectionFactory&version=1.0.1                

CommunityToolkit.ReflectionFactory

Overview

CommunityToolkit.ReflectionFactory is a C# library that provides a reflection-based factory for creating instances of classes marked with a custom attribute. This library is useful for scenarios where you need to dynamically create instances of classes at runtime based on certain criteria.

Features

  • Dynamic Instance Creation: Create instances of classes dynamically using reflection.
  • Custom Attributes: Use custom attributes to mark classes that can be instantiated by the factory.
  • Flexible Configuration: Configure the factory with module paths, assembly names, and type names.

Installation

To install the library, you can use NuGet Package Manager:

dotnet add package ReflectionFactory

Usage

Defining Classes with FactoryAttribute

First, define your classes and mark them with the FactoryAttribute:

using CommunityToolkit.ReflectionFactory;

[FactoryAttribute("Dog")]
public class Dog
{
    // Class implementation
}

[FactoryAttribute("Cat")]
public class Cat
{
    // Class implementation
}

Creating Instances with ReflectionFactory

Use the ReflectionFactory to create instances of your classes:

using CommunityToolkit.ReflectionFactory;

var dogFactory = new ReflectionFactory<Dog>();
var catFactory = new ReflectionFactory<Cat>();

var dog = dogFactory.GetInstance();
var cat = catFactory.GetInstance();

Unit Testing

You can write unit tests to verify the functionality of your factory:

using NUnit.Framework;
using CommunityToolkit.ReflectionFactory;

namespace TestProject
{
    public class ReflectionFactoryTests
    {
        [Test]
        public void GetInstance_ShouldReturnInstance()
        {
            // Arrange
            var dogFactory = new ReflectionFactory<Dog>();
            var catFactory = new ReflectionFactory<Cat>();

            // Act
            var dog = dogFactory.GetInstance();
            var cat = catFactory.GetInstance();

            // Assert
            Assert.IsNotNull(dog);
            Assert.IsInstanceOf<Dog>(dog);
            Assert.IsNotNull(cat);
            Assert.IsInstanceOf<Cat>(cat);
        }
    }
}

Contributing

Contributions are welcome! Please open an issue or submit a pull request on GitHub.

License

This project is licensed under the MIT License. See the LICENSE file for details.

Product Compatible and additional computed target framework versions.
.NET net6.0 is compatible.  net6.0-android was computed.  net6.0-ios was computed.  net6.0-maccatalyst was computed.  net6.0-macos was computed.  net6.0-tvos was computed.  net6.0-windows was computed.  net7.0 was computed.  net7.0-android was computed.  net7.0-ios was computed.  net7.0-maccatalyst was computed.  net7.0-macos was computed.  net7.0-tvos was computed.  net7.0-windows was computed.  net8.0 was computed.  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.  net9.0 was computed.  net9.0-android was computed.  net9.0-browser was computed.  net9.0-ios was computed.  net9.0-maccatalyst was computed.  net9.0-macos was computed.  net9.0-tvos was computed.  net9.0-windows was computed. 
Compatible target framework(s)
Included target framework(s) (in package)
Learn more about Target Frameworks and .NET Standard.
  • net6.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.