You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: README.md
+106-2Lines changed: 106 additions & 2 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -17,7 +17,7 @@ The fastest deep cloning library, supporting anything from <code>.NET 4.6</code>
17
17
## ✨ Features
18
18
19
19
-**The Fastest** - [Benchmarked](https://github.com/lofcz/FastCloner?tab=readme-ov-file#performance) to beat all other libraries with third-party independent benchmarks verifying the performance. **300x** speed-up vs `Newtonsoft.Json` and **160x** vs `System.Text.Json`
20
-
-**The Most Correct** - Cloning objects is hard: `<T>`, `abstract`, immutables, read-only, pointers, circular dependencies, deeply nested graphs.. we have over [500 tests](https://github.com/lofcz/FastCloner/tree/next/FastCloner.Tests) verifying correct behavior in these cases and we are transparent about the [limitations](https://github.com/lofcz/FastCloner?tab=readme-ov-file#limitations)
20
+
-**The Most Correct** - Cloning objects is hard: `<T>`, `abstract`, immutables, read-only, pointers, circular dependencies, deeply nested graphs.. we have over [600 tests](https://github.com/lofcz/FastCloner/tree/next/FastCloner.Tests) verifying correct behavior in these cases and we are transparent about the [limitations](https://github.com/lofcz/FastCloner?tab=readme-ov-file#limitations)
21
21
-**Novel Algorithm** - FastCloner recognizes that certain cloning code cannot be generated in certain scenarios and uses highly optimized reflection-based approach instead for these types - this only happens for the members that need this, not entire objects
22
22
-**Embeddable** - FastCloner has no dependencies outside the standard library. Source generator and reflection parts can be installed independently
23
23
-**Gentle & Caring** - FastCloner detects standard attributes like `[NonSerialized]` making it easy to try without polluting codebase with custom attributes. Type usage graph for generics is built automatically producing performant cloning code without manual annotations
@@ -101,6 +101,110 @@ Cache can be invalidated to reduce the memory footprint, if needed:
101
101
FastCloner.FastCloner.ClearCache();
102
102
```
103
103
104
+
### Generic Classes and Abstract Types
105
+
106
+
The source generator automatically discovers which concrete types your generic classes and abstract hierarchies are used with:
107
+
108
+
**Generic types** - The generator scans your codebase for usages like `MyClass<int>` or `MyClass<Customer>` and generates specialized cloning code:
109
+
110
+
```cs
111
+
[FastClonerClonable]
112
+
publicclassContainer<T>
113
+
{
114
+
publicTValue { get; set; }
115
+
}
116
+
117
+
// Source generator finds this usage and generates cloning code for Container<int>
118
+
varcontainer=newContainer<int> { Value=42 };
119
+
varclone=container.FastDeepClone();
120
+
```
121
+
122
+
**Abstract classes** - The generator automatically finds all concrete derived types in your codebase:
123
+
124
+
```cs
125
+
[FastClonerClonable]
126
+
publicabstractclassAnimal
127
+
{
128
+
publicstringName { get; set; }
129
+
}
130
+
131
+
publicclassDog : Animal
132
+
{
133
+
publicstringBreed { get; set; }
134
+
}
135
+
136
+
publicclassCat : Animal
137
+
{
138
+
publicboolIsIndoor { get; set; }
139
+
}
140
+
141
+
// Cloning via the abstract type works - the generator discovered Dog and Cat
For advanced scenarios, create a custom cloning context to explicitly register types you want to clone. This is useful when you need a centralized cloning entry point or want to clone types from external assemblies:
// Try to clone (returns false for unregistered types)
202
+
if (ctx.TryClone(obj, outvarcloned))
203
+
{
204
+
// Successfully cloned
205
+
}
206
+
```
207
+
104
208
## Limitations
105
209
106
210
- Cloning unmanaged resources, such as `IntPtr`s may result in side-effects, as there is no metadata for the length of buffers such pointers often point to.
@@ -137,7 +241,7 @@ You can run the benchmark [locally](https://github.com/lofcz/FastCloner/blob/nex
137
241
138
242
## Contributing
139
243
140
-
If you are looking to add new functionality, please open an issue first to verify your intent is aligned with the scope of the project. The library is covered by over [500 tests](https://github.com/lofcz/FastCloner/tree/next/src/FastCloner.Tests), please run them against your work before proposing changes. When reporting issues, providing a minimal reproduction we can plug in as a new test greatly reduces turnaround time.
244
+
If you are looking to add new functionality, please open an issue first to verify your intent is aligned with the scope of the project. The library is covered by over [600 tests](https://github.com/lofcz/FastCloner/tree/next/src/FastCloner.Tests), please run them against your work before proposing changes. When reporting issues, providing a minimal reproduction we can plug in as a new test greatly reduces turnaround time.
0 commit comments