ReflectionFactory 1.1.0
See the version list below for details.
dotnet add package ReflectionFactory --version 1.1.0
NuGet\Install-Package ReflectionFactory -Version 1.1.0
<PackageReference Include="ReflectionFactory" Version="1.1.0" />
paket add ReflectionFactory --version 1.1.0
#r "nuget: ReflectionFactory, 1.1.0"
// Install ReflectionFactory as a Cake Addin #addin nuget:?package=ReflectionFactory&version=1.1.0 // Install ReflectionFactory as a Cake Tool #tool nuget:?package=ReflectionFactory&version=1.1.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:
- Define your classes with the
Factory
attribute. - 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
using CommunityToolkit.ReflectionFactory;
namespace Example;
public class Program
{
public static void Main()
{
var foxFactory = new ReflectionFactory<Fox>();
var catFactory = new ReflectionFactory<Cat>();
var dogFactory = new ReflectionFactory<Dog>();
var fox = foxFactory.GetInstance();
var cat = catFactory.GetInstance();
var dog = dogFactory.GetInstance();
fox.Say(); // Output: What does Fox say!
cat.Say(); // Output: meow
dog.Say(); // Output: woof
}
}
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.
- Parameters:
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 | Versions 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. |
-
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.