ReflectionFactory 2.0.0

dotnet add package ReflectionFactory --version 2.0.0                
NuGet\Install-Package ReflectionFactory -Version 2.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="ReflectionFactory" Version="2.0.0" />                
For projects that support PackageReference, copy this XML node into the project file to reference the package.
paket add ReflectionFactory --version 2.0.0                
#r "nuget: ReflectionFactory, 2.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 ReflectionFactory as a Cake Addin
#addin nuget:?package=ReflectionFactory&version=2.0.0

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

CommunityToolkit.ReflectionFactory

The CommunityToolkit.ReflectionFactory is a library designed to dynamically create instances of types using reflection. It leverages a concurrent dictionary to cache assemblies, improving performance by avoiding repeated loading of the same assemblies.

Features

  • Dynamic Instance Creation: Create instances of types at runtime using reflection.
  • Assembly Caching: Cache loaded assemblies to optimize performance.
  • Custom Attribute Matching: Match types based on custom attributes.

Installation

To install the CommunityToolkit.ReflectionFactory library, add the following package to your project:

dotnet add package ReflectionFactory

Usage

Creating Instances

To create an instance of a type using the ReflectionFactory<T>, follow these steps:

  1. Define your classes with the Factory attribute.
  2. Use the ReflectionFactory<T> to create instances of these classes.

Example

Define Classes
using CommunityToolkit.ReflectionFactory;

namespace Example;

[Factory("Fox")]
public class Fox : Pet
{
    public void Say()
    {
        Console.WriteLine("What does Fox say!");
    }
}

[Factory]
public class Cat : Pet
{
    public void Say()
    {
        Console.WriteLine("meow");
    }
}

[Factory("Dog")]
public class Dog : Pet
{
    public void Say()
    {
        Console.WriteLine("woof");
    }
}
Create Instances
// See https://aka.ms/new-console-template for more information

using CommunityToolkit.ReflectionFactory;
using Example;
using TestLibrary;

Console.WriteLine("Hello, World!");
//通过type实例化
var dog = new ReflectionFactory<Dog>().GetInstance();
dog.Say();

var cat = new ReflectionFactory<Cat>().GetInstance();
cat.Say();

//通过名称实例化
var fox = new ReflectionFactory<Pet>().GetInstance(name: "Fox");
fox.Say();

//通过名称实例化
var chicken = new ReflectionFactory<Pet>().GetInstance(name: "Chicken", "KFC");
chicken.Say();

//依赖Assembly实例化
var sampleClass = new ReflectionFactory<SampleClass>().GetInstance(modulePath: null, args: "World");
sampleClass.Do();

//外部dll实例化
dynamic sampleClass2 = new ReflectionFactory<object>().GetInstance(
    modulePath: "C:\\Repos\\CommunityToolkit.ReflectionFactory\\TestLibrary\\bin\\Debug\\net6.0\\TestLibrary.dll",
    "some", args: "World 2");
sampleClass2.Do();

var factory = new DelegateFactory<SampleClass>();
var instance = factory.GetInstance("SampleClass", new object[] { "test" });
instance.Do();

Console.ReadLine();

API Reference

ReflectionFactory<T>

Methods
  • GetInstance: Creates an instance of the specified type using reflection.
    • Parameters:
      • modulePath (optional): The file path of the module containing the assembly.
      • assemblyName (optional): The name of the assembly.
      • name (optional): The name of the type to create an instance of.
    • Returns: An instance of the specified type.
    • Exceptions: Throws an exception if the type cannot be instantiated.

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.