|
| 1 | +# FastGenericNew [](https://github.com/Nyerst/FastGenericNew/actions/workflows/tests.yml) [](https://www.nuget.org/packages/FastGenericNew/) [](https://www.nuget.org/packages/FastGenericNew.SourceGenerator/) |
| 2 | + |
| 3 | +The ultimate fast and powerful alternative to `Activator.CreateInstance<T>` / `new T()` |
| 4 | + |
| 5 | +## ✨ Features |
| 6 | + |
| 7 | +- ✔️ **The best** `CreateInstance` ever |
| 8 | + - Up to 50x faster than `Activator.CreateInstance<T>` |
| 9 | + - Generic Parameters Support |
| 10 | + - Zero boxing/unboxing |
| 11 | + - TryGetValue-like TryFastNew API |
| 12 | + - Link Mode `PublishTrimmed` Support |
| 13 | + - Non-Public Constructor Support |
| 14 | + - No Generic Constraints |
| 15 | + - Compatible with .NET Standard 2.0 |
| 16 | + - Multiple backend implementations |
| 17 | + - Heavily tested on Win/Mac/Linux |
| 18 | + |
| 19 | +- 🪛 **Modern** Compiler Integration |
| 20 | + - Source Generator v2 (Incremental Generator) |
| 21 | + - Highly Configurable ([Props](https://github.com/Nyrest/FastGenericNew/wiki/SourceGenerator-Options)) |
| 22 | + - Multi-threaded Generation |
| 23 | + |
| 24 | +- 🔥 **Latest** C#/.NET Features Support |
| 25 | + - [C# 8 Nullable](https://docs.microsoft.com/en-us/dotnet/csharp/language-reference/builtin-types/nullable-reference-types) Support |
| 26 | + - [C# 10 Parameterless struct constructors](https://docs.microsoft.com/en-us/dotnet/csharp/language-reference/proposals/csharp-10.0/parameterless-struct-constructors) Support (Both invokes or not) |
| 27 | + - WebAssembly Support |
| 28 | + |
| 29 | +## 🔧 Installation |
| 30 | + |
| 31 | +> You should only use one of them |
| 32 | +
|
| 33 | +### Pre-Compiled Version |
| 34 | + |
| 35 | +```powershell |
| 36 | +dotnet add package FastGenericNew --version 3.3.0 |
| 37 | +``` |
| 38 | + |
| 39 | +```xml |
| 40 | +<ItemGroup> |
| 41 | + <PackageReference Include="FastGenericNew" Version="3.3.0" /> |
| 42 | +</ItemGroup> |
| 43 | +``` |
| 44 | + |
| 45 | +### SourceGenerator Version |
| 46 | + |
| 47 | +```powershell |
| 48 | +dotnet add package FastGenericNew.SourceGenerator --version 3.3.0 |
| 49 | +``` |
| 50 | + |
| 51 | +```xml |
| 52 | +<ItemGroup> |
| 53 | + <PackageReference Include="FastGenericNew.SourceGenerator" Version="3.3.0" /> |
| 54 | +</ItemGroup> |
| 55 | +``` |
| 56 | +#### SourceGeneratorV2 requires |
| 57 | +> ***.NET Standard 2.0*** or above |
| 58 | +> ***C# 8.0*** or above |
| 59 | +> ***Roslyn 4.0.1*** or above |
| 60 | +> ***Modern IDE*** *(Optional)* [VS2022, Rider, VSCode] |
| 61 | +
|
| 62 | +## 📖 Examples |
| 63 | + |
| 64 | +```cs |
| 65 | +using FastGenericNew; |
| 66 | + |
| 67 | +// Simply replace 'Activator' to 'FastNew' |
| 68 | +var obj = FastNew.CreateInstance<T>(); |
| 69 | + |
| 70 | +// With parameter(s) |
| 71 | +var obj2 = FastNew.CreateInstance<T, string>("text"); |
| 72 | +var obj3 = FastNew.CreateInstance<T, string, int>("text", 0); |
| 73 | + |
| 74 | +// Try pattern |
| 75 | +// NOTE: The try pattern will only check if the constructor can be called. |
| 76 | +// It will not catch or handle any exceptions thrown in the constructor. |
| 77 | +if (FastNew.TryCreateInstance<T, string>("arg0", out T result)); |
| 78 | +{ |
| 79 | + // ... |
| 80 | +} |
| 81 | +``` |
| 82 | + |
| 83 | +### Notes |
| 84 | + |
| 85 | +> **With .NET Framework**, `Activator.CreateInstance<T>()` invokes the parameterless constructor of **ValueType** if |
| 86 | +> the constraint is `where T : new()` but appears to **ignore the parameterless constructor if the constraint is `where T : struct`**. |
| 87 | +> **But `FastNew.CreateInstance<T>()` will always invoke the parameterless constructor if it's available.** |
| 88 | +> |
| 89 | +> If you don't want to invoke the parameterless constructor of **ValueType**, |
| 90 | +> consider using `FastNew.NewOrDefault<T>()` which **will never invoke the parameterless constructor of `ValueType`** |
| 91 | +
|
| 92 | +## 🚀 Benchmark |
| 93 | + |
| 94 | +### **Environment** |
| 95 | + |
| 96 | +``` ini |
| 97 | +BenchmarkDotNet=v0.13.1, OS=Windows 10.0.22000 |
| 98 | +AMD Ryzen 9 3900X, 1 CPU, 24 logical and 12 physical cores |
| 99 | +.NET SDK=6.0.200-preview.22055.15 |
| 100 | + [Host] : .NET 6.0.2 (6.0.222.6406), X64 RyuJIT |
| 101 | + .NET 5.0 : .NET 5.0.14 (5.0.1422.5710), X64 RyuJIT |
| 102 | + .NET 6.0 : .NET 6.0.2 (6.0.222.6406), X64 RyuJIT |
| 103 | + .NET Framework 4.8 : .NET Framework 4.8 (4.8.4470.0), X64 RyuJIT |
| 104 | +``` |
| 105 | + |
| 106 | +### Reference Types |
| 107 | + |
| 108 | +[](https://github.com/Nyrest/FastGenericNew/blob/main/FastGenericNew.Benchmarks/Benchmarks/ReferenceTypeBenchmark.cs) |
| 109 | + |
| 110 | +### Value Types |
| 111 | + |
| 112 | +[](https://github.com/Nyrest/FastGenericNew/blob/main/FastGenericNew.Benchmarks/Benchmarks/ValueTypeBenchmark.cs) |
| 113 | + |
| 114 | +## 📜 License |
| 115 | + |
| 116 | +FastGenericNew is licensed under the [MIT](LICENSE) license. |
0 commit comments