fahrizalfhrl/fast-generic-new
FastGenericNew: high-performance generic instantiation for .NET, replacing Activator.CreateInstance<
⚡ Fast Generic Instantiation Without the Reflection Tax
Replace Activator.CreateInstance<T> with cached delegates that run at full .NET speed.
FastGenericNew is a drop-in .NET library that provides high-performance generic instantiation, replacing Activator.CreateInstance<T> and new T() with cached, strongly-typed delegates and compile-time code paths. It supports generic parameters, non-public constructors, and parameterless struct constructors without boxing or generic constraints, making it ideal for performance-sensitive codebases that need to construct generic types efficiently.
✨ Highlights
- Drop-in Activator replacement:
FastNew.CreateInstance<T>()resolves to a cached, strongly-typed delegate at first use; subsequent calls are a single indirect invocation with no reflection overhead. - Generic arity up to 16 arguments:
FastNew.CreateInstance<T, TArg0>(...)throughFastNew.CreateInstance<T, ..., TArg15>(...)avoidsobject[]argument packing. - Zero boxing on value types: constructed via typed delegates per closed generic, so
Tstructs are returned without intermediateobjectallocation. - Try-pattern API:
FastNew.TryCreateInstance<T>(out T result)and generic-arg overloads returnboolwithout throwing on missing constructors. - Non-public constructor access: resolves
private/internal/protectedparameterless and parameterized constructors where the runtime permits. - No generic constraints required: usable with
Tlackingnew(),struct, orclassconstraints; unconstrained call sites are supported. - Source generator variant: incremental
IIncrementalGeneratoremits construction delegates at compile time, trim-safe under AOT and linker analysis. - Multiple backend implementations: expression-tree, dynamic-method/IL, and delegate-based paths selected per runtime and type shape; the fastest viable backend is chosen automatically.
💡 Why it matters
The standard Activator.CreateInstance<T> path relies on reflection, which is slow, allocates, and breaks in trimmed/AOT scenarios. FastGenericNew eliminates that overhead by caching delegates per closed generic type and offering a compile-time source generator that emits construction code at build time. The result is near-native instantiation speed with zero runtime reflection, safe under PublishTrimmed, AOT, and WebAssembly.
The library also solves common pain points: it works with unconstrained generic types, avoids boxing on value types, and provides a non-throwing TryCreateInstance API. Whether you choose the runtime package or the source generator variant, the API surface is identical — so you can switch without touching your call sites.
#DotNet #CSharp #Performance #OpenSource #TiniX
Similar Projects
Discover other similar projects and AI models.
Roadmap to becoming an ASP.NET Core developer in 2026
The ultimate fast alternative to Activator.CreateInstance<T> / new T()
Free, open-source file converter. Convert 263 formats offline by drag & drop — images, PDF, Word, Excel, PowerPoint, HWP/HWPX, video, audio, e-books and archives. No upload, no Office needed.
Develop Desktop, Embedded, Mobile and WebAssembly apps with C# and XAML. The future of .NET UI
A collection of awesome .NET libraries, tools, frameworks and software