A .NET library that scans assemblies for types implementing a specified base type, creates instances using reflection, and populates them with realistic fake data using the Bogus library.
- Assembly Scanning: Automatically discovers all types that implement or inherit from a specified base type
- Instance Creation: Creates instances using reflection without invoking constructors
- Fake Data Population: Populates object properties with realistic fake data using Bogus
- JSON Serialization: Generates JSON representations of created instances
- Custom Filtering: Supports custom type filters for fine-grained control over which types to include
- Generic Design: Works with any base type, class, or interface
- .NET 8.0 or higher
-
Clone the repository:
git clone https://github.com/Rad1c/ImplementationScanner.git cd ImplementationScanner
-
Build the solution:
dotnet build
-
Run the demo:
dotnet run --project EventLibraryGenerator
using EventLibraryGenerator;
using EventLibraryGenerator.Demo;
// Generate instances of all types that implement BaseEvent
var events = ImplementationScanner<BaseEvent>.GenerateEvents();
Console.WriteLine($"Generated {events.Count} events.");
// Generate JSON representation
string json = ImplementationScanner<BaseEvent>.GenerateEventsJsonString();
Console.WriteLine(json);
// Filter to only include types whose names end with "Event"
ImplementationScanner<BaseEvent>.SetTypeFilter(t =>
typeof(BaseEvent).IsAssignableFrom(t) &&
t.Name.EndsWith("Event") &&
!t.IsAbstract);
var filteredEvents = ImplementationScanner<BaseEvent>.GenerateEvents();
using System.Transactions;
// Works with built-in .NET types too
var transactions = ImplementationScanner<Transaction>.GenerateEvents();
Console.WriteLine($"Generated {transactions.Count} transactions.");
Returns a list of instances of all types that implement TBase
.
Returns: List<TBase>
- List of populated instances
Generates a JSON string representation of all instances.
Returns: string
- JSON array containing all instances
Sets a custom filter to control which types are included.
Parameters:
filter
- A function that takes aType
and returnsbool
Creates and populates a single instance of the specified type.
Parameters:
eventType
- The type to instantiatefaker
- Bogus faker instance for generating fake data
Returns: object?
- Populated instance or null if creation failed
The library automatically generates appropriate fake data for common .NET types:
string
- Random wordsint
- Random integers (1-1000)decimal
- Random decimals (10-1000)Guid
- New GUIDsDateTime
- Random dates- Complex objects - Recursively populated
- Collections - Populated with fake items
namespace EventLibraryGenerator.Demo;
public abstract class BaseEvent
{
// Base event class
}
public class UserCreatedEvent : BaseEvent
{
public string Username { get; set; }
public string Email { get; set; }
}
Running the demo application produces output like:
Generated 1 events.
[{
"Username": "sed",
"Email": "et"
}]
Generated 1 filtered events.
Generated 3 transactions.
- Assembly Scanning: Uses reflection to scan all loaded assemblies for types
- Type Filtering: Applies built-in and custom filters to select relevant types
- Instance Creation: Uses
FormatterServices.GetUninitializedObject()
to create instances without calling constructors - Property Population: Iterates through public properties and sets them with fake data
- Circular Reference Handling: Tracks visited types to prevent infinite recursion
- JSON Serialization: Uses
System.Text.Json
for JSON output
- Bogus (v35.6.3) - For generating fake data
Contributions are welcome! Please feel free to submit issues and pull requests.
This project is licensed under the MIT License.