HL.Object 2.0.9
HL.Object
A lightweight object factory library for dynamic object creation and instantiation in .NET Framework applications.
Overview
HL.Object provides the ObjectFactory class that simplifies dynamic object creation from type names, supporting both in-assembly and external assembly scenarios. It automatically handles assembly resolution and path management for DLL loading.
Features
- Dynamic Object Creation: Create objects from string type names at runtime
- Flexible Type Resolution: Support for simple type names and fully qualified assembly names
- Automatic Path Management: Intelligently resolves assembly paths (application directory or bin folder)
- External Assembly Support: Load types from external DLL files
- Simple API: Single static method for all object creation scenarios
Installation
NuGet Package Manager
Install-Package HL.Object
.NET CLI
dotnet add package HL.Object
Usage
Basic Usage - Create from Current Assembly
using HL.Object;
// Create an instance from the calling assembly
var obj = ObjectFactory.CreateInstance("MyNamespace.MyClass");
Create from External Assembly
// Create an instance from an external DLL
// Format: "Namespace.ClassName, AssemblyName.dll"
var obj = ObjectFactory.CreateInstance("MyNamespace.MyClass, MyAssembly.dll");
Example Scenarios
// Example 1: Simple type from current assembly
var logger = ObjectFactory.CreateInstance("MyApp.Logging.FileLogger");
// Example 2: Type from external plugin DLL
var plugin = ObjectFactory.CreateInstance("Plugins.DataProcessor, DataProcessorPlugin.dll");
// Example 3: Cast to expected type
var service = ObjectFactory.CreateInstance("Services.DataService") as IDataService;
How It Works
- Path Resolution: Automatically determines the correct path (AppDomain base directory or bin folder)
- Assembly Detection: Identifies whether to use the calling assembly or load an external DLL
- Type Parsing: Parses the type name string to separate namespace/class from assembly information
- Instance Creation: Uses reflection to create and return the object instance
Requirements
- .NET Framework 4.0 or higher
API Reference
ObjectFactory Class
Methods
CreateInstance(string typeName)
Creates an instance of the specified type.
Parameters:
typeName(string): The type name to instantiate. Can be:- Simple type name:
"Namespace.ClassName" - Full type name with assembly:
"Namespace.ClassName, AssemblyName.dll"
- Simple type name:
Returns:
object: The created instance, ornullif creation fails
Example:
object instance = ObjectFactory.CreateInstance("MyApp.Services.EmailService, EmailPlugin.dll");
Best Practices
Type Safety: Always cast or use pattern matching when using the returned objects
if (ObjectFactory.CreateInstance("MyService, Plugin.dll") is IService service) { service.Execute(); }Assembly Names: When specifying external assemblies, include the
.dllextensionvar obj = ObjectFactory.CreateInstance("Type, Assembly.dll");Error Handling: Check for null returns as creation may fail
var obj = ObjectFactory.CreateInstance(typeName); if (obj == null) { // Handle creation failure }
Use Cases
- Plugin Architectures: Dynamically load and instantiate plugin implementations
- Configuration-Driven Design: Create objects based on configuration files
- Factory Pattern Implementation: Simplify factory pattern implementations
- Dependency Injection: Build lightweight DI containers
- Modular Applications: Load modules and components at runtime
Technical Details
Assembly Loading Strategy
- Checks if assembly DLL name is specified in the type string
- If no DLL specified, uses the calling assembly
- If DLL specified and different from calling assembly, loads from disk
- Supports both absolute and relative paths with automatic bin folder detection
Path Resolution
The factory automatically resolves the correct path:
- Checks for
binsubfolder in application directory - Falls back to application base directory if no bin folder exists
- Caches the resolved path for performance
License
MIT License
Contributing
Contributions are welcome! Please feel free to submit issues or pull requests.
Related Packages
- HL.MCP: Manufacturing Control Platform framework
- HL.Common: Common utilities and helpers
Support
For issues, questions, or contributions, please visit the GitHub repository.
No packages depend on HL.Object.
.NET Framework 4.6.2
- No dependencies.
| Version | Downloads | Last updated |
|---|---|---|
| 2.0.9 | 0 | 08/19/2026 |