- Notifications
You must be signed in to change notification settings - Fork10
The ultimate fast alternative to Activator.CreateInstance<T> / new T()
License
Nyrest/FastGenericNew
Folders and files
| Name | Name | Last commit message | Last commit date | |
|---|---|---|---|---|
Repository files navigation
✔️The best
CreateInstanceever- Up to 50x faster than
Activator.CreateInstance<T> - Generic Parameters Support
- Zero boxing/unboxing
- TryGetValue-like TryFastNew API
- Link Mode
PublishTrimmedSupport - Non-Public Constructor Support
- No Generic Constraints
- Compatible with .NET Standard 2.0
- Multiple backend implementations
- Heavily tested on Win/Mac/Linux
- Up to 50x faster than
🪛Modern Compiler Integration
- Source Generator v2 (Incremental Generator)
- Highly Configurable (Props)
- Multi-threaded Generation
🔥Latest C#/.NET Features Support
- C# 8 Nullable Support
- C# 10 Parameterless struct constructors Support (Both invokes or not)
- WebAssembly Support
You should only use one of them
dotnet add package FastGenericNew--version3.3.1
<ItemGroup> <PackageReferenceInclude="FastGenericNew"Version="3.3.1" /></ItemGroup>
dotnet add package FastGenericNew.SourceGenerator--version3.3.1
<ItemGroup> <PackageReferenceInclude="FastGenericNew.SourceGenerator"Version="3.3.1" /></ItemGroup>
.NET Standard 2.0 or above
C# 8.0 or above
Roslyn 4.0.1 or above
Modern IDE(Optional) [VS2022, Rider, VSCode]
usingFastGenericNew;// Simply replace 'Activator' to 'FastNew'varobj=FastNew.CreateInstance<T>();// With parameter(s)varobj2=FastNew.CreateInstance<T,string>("text");varobj3=FastNew.CreateInstance<T,string,int>("text",0);// Try pattern// NOTE: The try pattern will only check if the constructor can be called.// It will not catch or handle any exceptions thrown in the constructor.if(FastNew.TryCreateInstance<T,string>("arg0",outTresult));{// ...}
With .NET Framework,
Activator.CreateInstance<T>()invokes the parameterless constructor ofValueType if
the constraint iswhere T : new()but appears toignore the parameterless constructor if the constraint iswhere T : struct.
ButFastNew.CreateInstance<T>()will always invoke the parameterless constructor if it's available.If you don't want to invoke the parameterless constructor ofValueType,consider using
FastNew.NewOrDefault<T>()whichwill never invoke the parameterless constructor ofValueType
Check the full benchmark results here:
https://github.com/Nyrest/FastGenericNew/blob/main/benchmark_results.md
BenchmarkDotNet v0.14.0, Windows 11 (10.0.22631.3958/23H2/2023Update/SunValley3)AMD Ryzen 9 3900X, 1 CPU, 24 logical and 12 physical cores.NET SDK 9.0.100-preview.7.24407.12 [Host] : .NET 8.0.3 (8.0.324.11423), X64 RyuJIT AVX2 Job-GFVMQQ : .NET 5.0.17 (5.0.1722.21314), X64 RyuJIT AVX2 Job-FGYWFO : .NET 6.0.33 (6.0.3324.36610), X64 RyuJIT AVX2 Job-LODQQQ : .NET 8.0.3 (8.0.324.11423), X64 RyuJIT AVX2 Job-NXJWMD : .NET 9.0.0 (9.0.24.40507), X64 RyuJIT AVX2 Job-VBBRLS : .NET Framework 4.8.1 (4.8.9256.0), X64 RyuJIT VectorSize=256FastGenericNew is licensed under theMIT license.
About
The ultimate fast alternative to Activator.CreateInstance<T> / new T()
Topics
Resources
License
Uh oh!
There was an error while loading.Please reload this page.
Stars
Watchers
Forks
Contributors4
Uh oh!
There was an error while loading.Please reload this page.

