├── src └── DeepCopy │ ├── DeepCopyDelegate.cs │ ├── ImmutableAttribute.cs │ ├── ReferenceEqualsComparer.cs │ ├── Immutable.cs │ ├── CopyContext.cs │ ├── DeepCopier.cs │ ├── DeepCopy.csproj │ ├── MethodInfos.cs │ ├── ArrayCopier.cs │ ├── CopierGenerator.cs │ └── CopyPolicy.cs ├── test ├── DeepCopy.Benchmarks │ ├── DeepCopy.Benchmarks.csproj │ ├── Program.cs │ └── GetCloneBenchmarks.cs └── DeepCopy.UnitTests │ ├── DeepCopy.UnitTests.csproj │ ├── BenchmarkTests.cs │ └── CopyTests.cs ├── README.md ├── LICENSE ├── .gitattributes ├── DeepCopy.sln └── .gitignore /src/DeepCopy/DeepCopyDelegate.cs: -------------------------------------------------------------------------------- 1 | namespace DeepCopy 2 | { 3 | /// 4 | /// Deep copier delegate. 5 | /// 6 | /// Original object to be deep copied. 7 | /// The context. 8 | /// Deep copy of the original object. 9 | internal delegate T DeepCopyDelegate(T original, CopyContext context); 10 | } -------------------------------------------------------------------------------- /test/DeepCopy.Benchmarks/DeepCopy.Benchmarks.csproj: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | Exe 5 | netcoreapp2.0 6 | 7 | 8 | 9 | 10 | 11 | 12 | 13 | 14 | 15 | 16 | 17 | 18 | -------------------------------------------------------------------------------- /src/DeepCopy/ImmutableAttribute.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | 3 | namespace DeepCopy 4 | { 5 | /// 6 | /// The Immutable attribute indicates that instances of the marked class or struct are never modified 7 | /// after they are created. 8 | /// 9 | /// 10 | /// Note that this implies that sub-objects are also not modified after the instance is created. 11 | /// 12 | [AttributeUsage(AttributeTargets.Struct | AttributeTargets.Class)] 13 | public sealed class ImmutableAttribute : Attribute 14 | { 15 | } 16 | } -------------------------------------------------------------------------------- /test/DeepCopy.UnitTests/DeepCopy.UnitTests.csproj: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | netcoreapp2.0 5 | 6 | false 7 | 8 | 9 | 10 | 11 | 12 | 13 | 14 | 15 | 16 | 17 | 18 | 19 | 20 | -------------------------------------------------------------------------------- /src/DeepCopy/ReferenceEqualsComparer.cs: -------------------------------------------------------------------------------- 1 | using System.Collections.Generic; 2 | using System.Runtime.CompilerServices; 3 | 4 | namespace DeepCopy 5 | { 6 | /// 7 | internal sealed class ReferenceEqualsComparer : IEqualityComparer 8 | { 9 | /// 10 | /// Gets an instance of this class. 11 | /// 12 | public static ReferenceEqualsComparer Instance { get; } = new ReferenceEqualsComparer(); 13 | 14 | /// 15 | bool IEqualityComparer.Equals(object x, object y) 16 | { 17 | return object.ReferenceEquals(x, y); 18 | } 19 | 20 | /// 21 | int IEqualityComparer.GetHashCode(object obj) 22 | { 23 | return obj == null ? 0 : RuntimeHelpers.GetHashCode(obj); 24 | } 25 | } 26 | } -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # DeepCopy 2 | Simple & efficient library for deep copying .NET objects 3 | 4 | Described in this blog post: https://reubenbond.github.io/posts/codegen-2-il-boogaloo 5 | 6 | ## Installation: 7 | Install via NuGet: 8 | ```powershell 9 | PM> Install-Package DeepCopy 10 | ``` 11 | 12 | ## Usage: 13 | ```C# 14 | // Add a using directive for DeepCopy. 15 | var poco = new Poco(); 16 | var original = new[] { poco, poco }; 17 | 18 | var result = DeepCopier.Copy(original); 19 | 20 | // The result is a copy of the original. 21 | Assert.NotSame(original, result); 22 | 23 | // Because both elements in the original array point to the same object, 24 | // both elements in the copied array also point to the same object. 25 | Assert.Same(result[0], result[1]); 26 | ``` 27 | 28 | Optionally, classes can be marked using the `[Immutable]` attribute to tell `DeepCopy` to skip copying them and return them unmodified. 29 | Object can also be wrapped in `Immutable` using `Immutable.Create(value)`. 30 | 31 | The majority of this project was adapted from [`dotnet/orleans`](https://github.com/dotnet/orleans). 32 | 33 | PR's welcome! -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | MIT License 2 | 3 | Copyright (c) 2017 Reuben Bond 4 | 5 | Permission is hereby granted, free of charge, to any person obtaining a copy 6 | of this software and associated documentation files (the "Software"), to deal 7 | in the Software without restriction, including without limitation the rights 8 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 9 | copies of the Software, and to permit persons to whom the Software is 10 | furnished to do so, subject to the following conditions: 11 | 12 | The above copyright notice and this permission notice shall be included in all 13 | copies or substantial portions of the Software. 14 | 15 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 16 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 17 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 18 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 19 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 20 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 21 | SOFTWARE. 22 | -------------------------------------------------------------------------------- /test/DeepCopy.Benchmarks/Program.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | using BenchmarkDotNet.Configs; 3 | using BenchmarkDotNet.Diagnosers; 4 | using BenchmarkDotNet.Running; 5 | 6 | namespace DeepCopy.Benchmarks 7 | { 8 | class Program 9 | { 10 | static void Main(string[] args) 11 | { 12 | var config = ManualConfig.Create(DefaultConfig.Instance).With(MemoryDiagnoser.Default); 13 | BenchmarkRunner.Run(config); 14 | } 15 | } 16 | 17 | public class SimpleClassBase 18 | { 19 | public int BaseInt { get; set; } 20 | } 21 | 22 | public class SimpleClass : SimpleClassBase 23 | { 24 | public int Int { get; set; } 25 | public uint UInt { get; set; } 26 | public long Long { get; set; } 27 | public ulong ULong { get; set; } 28 | public double Double { get; set; } 29 | public float Float { get; set; } 30 | public string String { get; set; } 31 | } 32 | 33 | public struct SimpleStruct 34 | { 35 | public int Int { get; set; } 36 | public uint UInt { get; set; } 37 | public long Long { get; set; } 38 | } 39 | } 40 | -------------------------------------------------------------------------------- /src/DeepCopy/Immutable.cs: -------------------------------------------------------------------------------- 1 | namespace DeepCopy 2 | { 3 | /// 4 | /// Helper class for creating immutable values. 5 | /// 6 | public static class Immutable 7 | { 8 | /// 9 | /// Returns an immutable wrapper over the provided value. 10 | /// 11 | /// The value type. 12 | /// The value. 13 | /// An immutable wrapper over the provided value. 14 | public static Immutable Create(T value) => new Immutable(value); 15 | } 16 | 17 | /// 18 | /// Wrapper class for creating immutable values. 19 | /// 20 | /// The wrapped type. 21 | [Immutable] 22 | public struct Immutable 23 | { 24 | /// 25 | /// Initializes a new instance. 26 | /// 27 | /// The value. 28 | public Immutable(T value) 29 | { 30 | this.Value = value; 31 | } 32 | 33 | /// 34 | /// Gets the value held by this instance. 35 | /// 36 | public T Value { get; } 37 | } 38 | } -------------------------------------------------------------------------------- /src/DeepCopy/CopyContext.cs: -------------------------------------------------------------------------------- 1 | using System.Collections.Generic; 2 | 3 | namespace DeepCopy 4 | { 5 | /// 6 | /// Records details about copied objects. 7 | /// 8 | public sealed class CopyContext 9 | { 10 | private readonly Dictionary copies = new Dictionary(16, ReferenceEqualsComparer.Instance); 11 | 12 | /// 13 | /// Records as a copy of . 14 | /// 15 | /// The original object. 16 | /// The copy of . 17 | public void RecordCopy(object original, object copy) 18 | { 19 | copies[original] = copy; 20 | } 21 | 22 | /// 23 | /// Returns the copy of if it has been copied or if it has not yet been copied. 24 | /// 25 | /// The original object. 26 | /// The copied object. 27 | /// The copy of or if no copy has been made. 28 | public bool TryGetCopy(object original, out object result) 29 | { 30 | return copies.TryGetValue(original, out result); 31 | } 32 | 33 | /// 34 | /// Resets this instance so that it can be reused. 35 | /// 36 | internal void Reset() 37 | { 38 | copies.Clear(); 39 | } 40 | } 41 | } -------------------------------------------------------------------------------- /src/DeepCopy/DeepCopier.cs: -------------------------------------------------------------------------------- 1 | using System.Threading; 2 | 3 | namespace DeepCopy 4 | { 5 | /// 6 | /// Methods for creating deep copies of objects. 7 | /// 8 | public static class DeepCopier 9 | { 10 | internal static readonly CopyPolicy CopyPolicy = new CopyPolicy(); 11 | internal static readonly MethodInfos MethodInfos = new MethodInfos(); 12 | private static readonly ThreadLocal Context = new ThreadLocal(() => new CopyContext()); 13 | 14 | /// 15 | /// Creates and returns a deep copy of the provided object. 16 | /// 17 | /// The object type. 18 | /// The object to copy. 19 | /// A deep copy of the provided object. 20 | public static T Copy(T original) 21 | { 22 | var context = Context.Value; 23 | try 24 | { 25 | return CopierGenerator.Copy(original, context); 26 | } 27 | finally 28 | { 29 | context.Reset(); 30 | } 31 | } 32 | 33 | /// 34 | /// Creates and returns a deep copy of the provided object. 35 | /// 36 | /// The object type. 37 | /// The object to copy. 38 | /// 39 | /// The copy context, providing referential integrity between multiple calls to this method. 40 | /// 41 | /// A deep copy of the provided object. 42 | public static T Copy(T original, CopyContext context) 43 | { 44 | return CopierGenerator.Copy(original, context); 45 | } 46 | } 47 | } 48 | -------------------------------------------------------------------------------- /src/DeepCopy/DeepCopy.csproj: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | netstandard2.0 5 | https://github.com/ReubenBond/DeepCopy 6 | https://github.com/ReubenBond/DeepCopy/blob/master/LICENSE 7 | Reuben Bond, 2017 8 | Simple and efficient library for deep copying .NET objects 9 | deepcopy copy deep clone deepclone 10 | https://github.com/ReubenBond/DeepCopy 11 | Reuben Bond 12 | 13 | true 14 | 1.0.0.0 15 | 1.0.3 16 | true 17 | true 18 | snupkg 19 | 20 | 21 | 22 | 23 | 24 | 25 | 26 | 27 | bin\Release\netstandard2.0\DeepCopy.xml 28 | true 29 | 30 | 31 | 32 | bin\Debug\netstandard2.0\DeepCopy.xml 33 | true 34 | 35 | 36 | 37 | 38 | 39 | 40 | 41 | 42 | 43 | -------------------------------------------------------------------------------- /.gitattributes: -------------------------------------------------------------------------------- 1 | ############################################################################### 2 | # Set default behavior to automatically normalize line endings. 3 | ############################################################################### 4 | * text=auto 5 | 6 | ############################################################################### 7 | # Set default behavior for command prompt diff. 8 | # 9 | # This is need for earlier builds of msysgit that does not have it on by 10 | # default for csharp files. 11 | # Note: This is only used by command line 12 | ############################################################################### 13 | #*.cs diff=csharp 14 | 15 | ############################################################################### 16 | # Set the merge driver for project and solution files 17 | # 18 | # Merging from the command prompt will add diff markers to the files if there 19 | # are conflicts (Merging from VS is not affected by the settings below, in VS 20 | # the diff markers are never inserted). Diff markers may cause the following 21 | # file extensions to fail to load in VS. An alternative would be to treat 22 | # these files as binary and thus will always conflict and require user 23 | # intervention with every merge. To do so, just uncomment the entries below 24 | ############################################################################### 25 | #*.sln merge=binary 26 | #*.csproj merge=binary 27 | #*.vbproj merge=binary 28 | #*.vcxproj merge=binary 29 | #*.vcproj merge=binary 30 | #*.dbproj merge=binary 31 | #*.fsproj merge=binary 32 | #*.lsproj merge=binary 33 | #*.wixproj merge=binary 34 | #*.modelproj merge=binary 35 | #*.sqlproj merge=binary 36 | #*.wwaproj merge=binary 37 | 38 | ############################################################################### 39 | # behavior for image files 40 | # 41 | # image files are treated as binary by default. 42 | ############################################################################### 43 | #*.jpg binary 44 | #*.png binary 45 | #*.gif binary 46 | 47 | ############################################################################### 48 | # diff behavior for common document formats 49 | # 50 | # Convert binary document formats to text before diffing them. This feature 51 | # is only available from the command line. Turn it on by uncommenting the 52 | # entries below. 53 | ############################################################################### 54 | #*.doc diff=astextplain 55 | #*.DOC diff=astextplain 56 | #*.docx diff=astextplain 57 | #*.DOCX diff=astextplain 58 | #*.dot diff=astextplain 59 | #*.DOT diff=astextplain 60 | #*.pdf diff=astextplain 61 | #*.PDF diff=astextplain 62 | #*.rtf diff=astextplain 63 | #*.RTF diff=astextplain 64 | -------------------------------------------------------------------------------- /DeepCopy.sln: -------------------------------------------------------------------------------- 1 | 2 | Microsoft Visual Studio Solution File, Format Version 12.00 3 | # Visual Studio 15 4 | VisualStudioVersion = 15.0.27004.2006 5 | MinimumVisualStudioVersion = 10.0.40219.1 6 | Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "DeepCopy", "src\DeepCopy\DeepCopy.csproj", "{751D7E7F-D8AF-4B0E-93FE-F9CCFD250D7B}" 7 | EndProject 8 | Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "DeepCopy.UnitTests", "test\DeepCopy.UnitTests\DeepCopy.UnitTests.csproj", "{366726CE-A985-4989-83A9-0F1804EF1318}" 9 | EndProject 10 | Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "src", "src", "{B1EF27BC-A0BD-4084-94CF-D9B9DA7B760D}" 11 | EndProject 12 | Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "test", "test", "{20B6EB03-27D6-4E22-9727-2ABB66530260}" 13 | EndProject 14 | Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "DeepCopy.Benchmarks", "test\DeepCopy.Benchmarks\DeepCopy.Benchmarks.csproj", "{BF1B80AE-FD45-4211-84AE-6540544631E7}" 15 | EndProject 16 | Global 17 | GlobalSection(SolutionConfigurationPlatforms) = preSolution 18 | Debug|Any CPU = Debug|Any CPU 19 | Release|Any CPU = Release|Any CPU 20 | EndGlobalSection 21 | GlobalSection(ProjectConfigurationPlatforms) = postSolution 22 | {751D7E7F-D8AF-4B0E-93FE-F9CCFD250D7B}.Debug|Any CPU.ActiveCfg = Debug|Any CPU 23 | {751D7E7F-D8AF-4B0E-93FE-F9CCFD250D7B}.Debug|Any CPU.Build.0 = Debug|Any CPU 24 | {751D7E7F-D8AF-4B0E-93FE-F9CCFD250D7B}.Release|Any CPU.ActiveCfg = Release|Any CPU 25 | {751D7E7F-D8AF-4B0E-93FE-F9CCFD250D7B}.Release|Any CPU.Build.0 = Release|Any CPU 26 | {366726CE-A985-4989-83A9-0F1804EF1318}.Debug|Any CPU.ActiveCfg = Debug|Any CPU 27 | {366726CE-A985-4989-83A9-0F1804EF1318}.Debug|Any CPU.Build.0 = Debug|Any CPU 28 | {366726CE-A985-4989-83A9-0F1804EF1318}.Release|Any CPU.ActiveCfg = Release|Any CPU 29 | {366726CE-A985-4989-83A9-0F1804EF1318}.Release|Any CPU.Build.0 = Release|Any CPU 30 | {BF1B80AE-FD45-4211-84AE-6540544631E7}.Debug|Any CPU.ActiveCfg = Debug|Any CPU 31 | {BF1B80AE-FD45-4211-84AE-6540544631E7}.Debug|Any CPU.Build.0 = Debug|Any CPU 32 | {BF1B80AE-FD45-4211-84AE-6540544631E7}.Release|Any CPU.ActiveCfg = Release|Any CPU 33 | {BF1B80AE-FD45-4211-84AE-6540544631E7}.Release|Any CPU.Build.0 = Release|Any CPU 34 | EndGlobalSection 35 | GlobalSection(SolutionProperties) = preSolution 36 | HideSolutionNode = FALSE 37 | EndGlobalSection 38 | GlobalSection(NestedProjects) = preSolution 39 | {751D7E7F-D8AF-4B0E-93FE-F9CCFD250D7B} = {B1EF27BC-A0BD-4084-94CF-D9B9DA7B760D} 40 | {366726CE-A985-4989-83A9-0F1804EF1318} = {20B6EB03-27D6-4E22-9727-2ABB66530260} 41 | {BF1B80AE-FD45-4211-84AE-6540544631E7} = {20B6EB03-27D6-4E22-9727-2ABB66530260} 42 | EndGlobalSection 43 | GlobalSection(ExtensibilityGlobals) = postSolution 44 | SolutionGuid = {75411B3B-8856-46A2-9B9C-305434E64AB2} 45 | EndGlobalSection 46 | EndGlobal 47 | -------------------------------------------------------------------------------- /src/DeepCopy/MethodInfos.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | using System.Linq.Expressions; 3 | using System.Reflection; 4 | using System.Runtime.Serialization; 5 | 6 | namespace DeepCopy 7 | { 8 | /// 9 | /// Holds references to methods which are used during copying. 10 | /// 11 | internal sealed class MethodInfos 12 | { 13 | /// 14 | /// A reference to the method. 15 | /// 16 | public readonly MethodInfo TryGetCopy; 17 | 18 | /// 19 | /// A reference to the method. 20 | /// 21 | public readonly MethodInfo RecordObject; 22 | 23 | /// 24 | /// A reference to 25 | /// 26 | public readonly MethodInfo CopyInner; 27 | 28 | /// 29 | /// A reference to a method which returns an uninitialized object of the provided type. 30 | /// 31 | public readonly MethodInfo GetUninitializedObject; 32 | 33 | /// 34 | /// A reference to . 35 | /// 36 | public readonly MethodInfo GetTypeFromHandle; 37 | 38 | public readonly MethodInfo CopyArrayRank1Shallow; 39 | public readonly MethodInfo CopyArrayRank1; 40 | 41 | public readonly MethodInfo CopyArrayRank2Shallow; 42 | public readonly MethodInfo CopyArrayRank2; 43 | 44 | public MethodInfos() 45 | { 46 | this.GetUninitializedObject = GetFuncCall(() => FormatterServices.GetUninitializedObject(typeof(int))); 47 | this.GetTypeFromHandle = GetFuncCall(() => Type.GetTypeFromHandle(typeof(Type).TypeHandle)); 48 | this.CopyInner = GetFuncCall(() => DeepCopier.Copy(default(object), default(CopyContext))).GetGenericMethodDefinition(); 49 | this.TryGetCopy = typeof(CopyContext).GetMethod("TryGetCopy"); 50 | this.RecordObject = GetActionCall((CopyContext ctx) => ctx.RecordCopy(default(object), default(object))); 51 | 52 | this.CopyArrayRank1Shallow = GetFuncCall(() => ArrayCopier.CopyArrayRank1Shallow(default(object[]), default(CopyContext))).GetGenericMethodDefinition(); 53 | this.CopyArrayRank1 = GetFuncCall(() => ArrayCopier.CopyArrayRank1(default(object[]), default(CopyContext))).GetGenericMethodDefinition(); 54 | 55 | this.CopyArrayRank2Shallow = GetFuncCall(() => ArrayCopier.CopyArrayRank2Shallow(default(object[,]), default(CopyContext))).GetGenericMethodDefinition(); 56 | this.CopyArrayRank2 = GetFuncCall(() => ArrayCopier.CopyArrayRank2(default(object[,]), default(CopyContext))).GetGenericMethodDefinition(); 57 | 58 | MethodInfo GetActionCall(Expression> expression) 59 | { 60 | return (expression.Body as MethodCallExpression)?.Method 61 | ?? throw new ArgumentException("Expression type unsupported."); 62 | } 63 | 64 | MethodInfo GetFuncCall(Expression> expression) 65 | { 66 | return (expression.Body as MethodCallExpression)?.Method 67 | ?? throw new ArgumentException("Expression type unsupported."); 68 | } 69 | } 70 | } 71 | } -------------------------------------------------------------------------------- /test/DeepCopy.Benchmarks/GetCloneBenchmarks.cs: -------------------------------------------------------------------------------- 1 | using System.Collections.Generic; 2 | using System.Linq; 3 | using BenchmarkDotNet.Attributes; 4 | using CloneExtensions; 5 | 6 | namespace DeepCopy.Benchmarks 7 | { 8 | public class GetCloneBenchmarks 9 | { 10 | private readonly SimpleClass _simpleClass; 11 | private readonly List _listOfInts; 12 | private readonly List _listOfSimpleClassSameInstance; 13 | private readonly List _listOfSimpleClassDifferentInstances; 14 | private readonly List _listOfSimpleStruct; 15 | 16 | public GetCloneBenchmarks() 17 | { 18 | this._simpleClass = new SimpleClass() 19 | { 20 | Int = 10, 21 | UInt = 1231, 22 | Long = 1231234561L, 23 | ULong = 1516524352UL, 24 | Double = 1235.1235762, 25 | Float = 1.333F, 26 | String = "Lorem ipsum ..." 27 | }; 28 | 29 | this._listOfInts = Enumerable.Range(0, 10000).ToList(); 30 | 31 | this._listOfSimpleClassSameInstance = Enumerable.Repeat(this._simpleClass, 10000).ToList(); 32 | this._listOfSimpleClassDifferentInstances = Enumerable.Range(0, 10000).Select(x => new SimpleClass() {Int = x}).ToList(); 33 | this._listOfSimpleStruct = Enumerable.Range(0, 10000).Select(x => new SimpleStruct() {Int = x}).ToList(); 34 | } 35 | 36 | [Benchmark] 37 | public int SimpleClass_CloneExt() 38 | { 39 | var clone = this._simpleClass.GetClone(); 40 | return clone.Int; 41 | } 42 | 43 | [Benchmark] 44 | public int ListOfInts_CloneExt() 45 | { 46 | var clone = this._listOfInts.GetClone(); 47 | return clone.Count; 48 | } 49 | 50 | [Benchmark] 51 | public int ListOfSimpleClassSameInstance_CloneExt() 52 | { 53 | var clone = this._listOfSimpleClassSameInstance.GetClone(); 54 | return clone.Count; 55 | } 56 | 57 | [Benchmark] 58 | public int ListOfSimpleClassDifferentInstances_CloneExt() 59 | { 60 | var clone = this._listOfSimpleClassDifferentInstances.GetClone(); 61 | return clone.Count; 62 | } 63 | 64 | [Benchmark] 65 | public int ListOfStruct_CloneExt() 66 | { 67 | var clone = this._listOfSimpleStruct.GetClone(); 68 | return clone.Count; 69 | } 70 | 71 | [Benchmark] 72 | public int SimpleClass_DeepCopy() 73 | { 74 | var clone = DeepCopier.Copy(this._simpleClass); 75 | return clone.Int; 76 | } 77 | 78 | [Benchmark] 79 | public int ListOfInts_DeepCopy() 80 | { 81 | var clone = DeepCopier.Copy(this._listOfInts); 82 | return clone.Count; 83 | } 84 | 85 | [Benchmark] 86 | public int ListOfSimpleClassSameInstance_DeepCopy() 87 | { 88 | var clone = DeepCopier.Copy(this._listOfSimpleClassSameInstance); 89 | return clone.Count; 90 | } 91 | 92 | [Benchmark] 93 | public int ListOfSimpleClassDifferentInstances_DeepCopy() 94 | { 95 | var clone = DeepCopier.Copy(this._listOfSimpleClassDifferentInstances); 96 | return clone.Count; 97 | } 98 | 99 | [Benchmark] 100 | public int ListOfStruct_DeepCopy() 101 | { 102 | var clone = DeepCopier.Copy(this._listOfSimpleStruct); 103 | return clone.Count; 104 | } 105 | } 106 | } -------------------------------------------------------------------------------- /test/DeepCopy.UnitTests/BenchmarkTests.cs: -------------------------------------------------------------------------------- 1 | using System.Collections.Generic; 2 | using System.Diagnostics; 3 | using System.Linq; 4 | using Xunit; 5 | using Xunit.Abstractions; 6 | 7 | namespace DeepCopy.UnitTests 8 | { 9 | [Trait("TestCategory", "BenchmarkBVT")] 10 | public class BenchmarkTests 11 | { 12 | private readonly SimpleClass _simpleClass; 13 | private readonly List _listOfInts; 14 | private readonly List _listOfSimpleClassSameInstance; 15 | private readonly List _listOfSimpleClassDifferentInstances; 16 | private readonly List _listOfSimpleStruct; 17 | 18 | public BenchmarkTests() 19 | { 20 | this._simpleClass = new SimpleClass() 21 | { 22 | Int = 10, 23 | UInt = 1231, 24 | Long = 1231234561L, 25 | ULong = 1516524352UL, 26 | Double = 1235.1235762, 27 | Float = 1.333F, 28 | String = "Lorem ipsum ...", 29 | }; 30 | 31 | this._listOfInts = Enumerable.Range(0, 10000).ToList(); 32 | 33 | this._listOfSimpleClassSameInstance = Enumerable.Repeat(this._simpleClass, 10000).ToList(); 34 | this._listOfSimpleClassDifferentInstances = Enumerable.Range(0, 10000).Select(x => new SimpleClass() { Int = x }).ToList(); 35 | this._listOfSimpleStruct = Enumerable.Range(0, 10000).Select(x => new SimpleStruct() { Int = x }).ToList(); 36 | } 37 | 38 | public class SimpleClassBase 39 | { 40 | public int BaseInt { get; set; } 41 | } 42 | 43 | public class SimpleClass : SimpleClassBase 44 | { 45 | public int Int { get; set; } 46 | public uint UInt { get; set; } 47 | public long Long { get; set; } 48 | public ulong ULong { get; set; } 49 | public double Double { get; set; } 50 | public float Float { get; set; } 51 | public string String { get; set; } 52 | } 53 | 54 | public struct SimpleStruct 55 | { 56 | public int Int { get; set; } 57 | public uint UInt { get; set; } 58 | public long Long { get; set; } 59 | } 60 | 61 | [Fact] 62 | public void SimpleClass_DeepCopy() 63 | { 64 | var clone = DeepCopier.Copy(this._simpleClass); 65 | Assert.NotSame(clone, this._simpleClass); 66 | } 67 | 68 | [Fact] 69 | public void ListOfInts_DeepCopy() 70 | { 71 | var clone = DeepCopier.Copy(this._listOfInts); 72 | Assert.NotSame(clone, this._listOfInts); 73 | } 74 | 75 | [Fact] 76 | public void ListOfSimpleClassSameInstance_DeepCopy() 77 | { 78 | var clone = DeepCopier.Copy(this._listOfSimpleClassSameInstance); 79 | Assert.NotSame(clone, this._listOfSimpleClassSameInstance); 80 | var firstInstance = clone[0]; 81 | for (int i = 1; i < clone.Count; i++) 82 | { 83 | Assert.Same(firstInstance, clone[i]); 84 | } 85 | } 86 | 87 | [Fact] 88 | public void ListOfSimpleClassDifferentInstances_DeepCopy() 89 | { 90 | var clone = DeepCopier.Copy(this._listOfSimpleClassDifferentInstances); 91 | Assert.NotSame(clone, this._listOfSimpleClassDifferentInstances); 92 | var firstInstance = clone[0]; 93 | for (int i = 1; i < clone.Count; i++) 94 | { 95 | Assert.NotSame(firstInstance, clone[i]); 96 | } 97 | } 98 | 99 | [Fact] 100 | public void ListOfStruct_DeepCopy() 101 | { 102 | var clone = DeepCopier.Copy(this._listOfSimpleStruct); 103 | Assert.NotSame(clone, this._listOfSimpleClassDifferentInstances); 104 | } 105 | } 106 | } -------------------------------------------------------------------------------- /src/DeepCopy/ArrayCopier.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | 3 | namespace DeepCopy 4 | { 5 | internal static class ArrayCopier 6 | { 7 | internal static T[] CopyArrayRank1(T[] originalArray, CopyContext context) 8 | { 9 | if (context.TryGetCopy(originalArray, out var existingCopy)) return (T[]) existingCopy; 10 | 11 | var length = originalArray.Length; 12 | var result = new T[length]; 13 | context.RecordCopy(originalArray, result); 14 | for (var i = 0; i < length; i++) 15 | { 16 | var original = originalArray[i]; 17 | if (original != null) 18 | { 19 | if (context.TryGetCopy(original, out var existingElement)) 20 | { 21 | result[i] = (T) existingElement; 22 | } 23 | else 24 | { 25 | var copy = CopierGenerator.Copy(original, context); 26 | context.RecordCopy(original, copy); 27 | result[i] = copy; 28 | } 29 | } 30 | } 31 | return result; 32 | } 33 | 34 | internal static T[,] CopyArrayRank2(T[,] originalArray, CopyContext context) 35 | { 36 | if (context.TryGetCopy(originalArray, out var existingCopy)) return (T[,])existingCopy; 37 | 38 | var lenI = originalArray.GetLength(0); 39 | var lenJ = originalArray.GetLength(1); 40 | var result = new T[lenI, lenJ]; 41 | context.RecordCopy(originalArray, result); 42 | for (var i = 0; i < lenI; i++) 43 | { 44 | for (var j = 0; j < lenJ; j++) 45 | { 46 | var original = originalArray[i, j]; 47 | if (original != null) 48 | { 49 | if (context.TryGetCopy(original, out var existingElement)) 50 | { 51 | result[i, j] = (T) existingElement; 52 | } 53 | else 54 | { 55 | var copy = CopierGenerator.Copy(original, context); 56 | context.RecordCopy(original, copy); 57 | result[i, j] = copy; 58 | } 59 | } 60 | } 61 | } 62 | return result; 63 | } 64 | 65 | internal static T[] CopyArrayRank1Shallow(T[] array, CopyContext context) 66 | { 67 | if (context.TryGetCopy(array, out var existingCopy)) return (T[])existingCopy; 68 | 69 | var length = array.Length; 70 | var result = new T[length]; 71 | context.RecordCopy(array, result); 72 | Array.Copy(array, result, length); 73 | return result; 74 | } 75 | 76 | internal static T[,] CopyArrayRank2Shallow(T[,] array, CopyContext context) 77 | { 78 | if (context.TryGetCopy(array, out var existingCopy)) return (T[,])existingCopy; 79 | 80 | var lenI = array.GetLength(0); 81 | var lenJ = array.GetLength(1); 82 | var result = new T[lenI, lenJ]; 83 | context.RecordCopy(array, result); 84 | Array.Copy(array, result, array.Length); 85 | return result; 86 | } 87 | 88 | internal static T CopyArray(T array, CopyContext context) 89 | { 90 | if (context.TryGetCopy(array, out var existingCopy)) return (T)existingCopy; 91 | 92 | var originalArray = array as Array; 93 | if (originalArray == null) throw new InvalidCastException($"Cannot cast non-array type {array?.GetType()} to Array."); 94 | var elementType = array.GetType().GetElementType(); 95 | 96 | var rank = originalArray.Rank; 97 | var lengths = new int[rank]; 98 | for (var i = 0; i < rank; i++) 99 | { 100 | lengths[i] = originalArray.GetLength(i); 101 | } 102 | 103 | var copyArray = Array.CreateInstance(elementType, lengths); 104 | context.RecordCopy(originalArray, copyArray); 105 | 106 | if (DeepCopier.CopyPolicy.IsImmutable(elementType)) 107 | { 108 | Array.Copy(originalArray, copyArray, originalArray.Length); 109 | } 110 | 111 | var index = new int[rank]; 112 | var sizes = new int[rank]; 113 | sizes[rank - 1] = 1; 114 | for (var k = rank - 2; k >= 0; k--) 115 | { 116 | sizes[k] = sizes[k + 1] * lengths[k + 1]; 117 | } 118 | for (var i = 0; i < originalArray.Length; i++) 119 | { 120 | var k = i; 121 | for (var n = 0; n < rank; n++) 122 | { 123 | var offset = k / sizes[n]; 124 | k = k - offset * sizes[n]; 125 | index[n] = offset; 126 | } 127 | var original = originalArray.GetValue(index); 128 | if (original != null) 129 | { 130 | if (context.TryGetCopy(original, out var existingElement)) 131 | { 132 | copyArray.SetValue(existingElement, index); 133 | } 134 | else 135 | { 136 | var copy = DeepCopier.Copy(originalArray.GetValue(index), context); 137 | context.RecordCopy(original, copy); 138 | copyArray.SetValue(copy, index); 139 | } 140 | } 141 | } 142 | 143 | return (T)(object)copyArray; 144 | } 145 | } 146 | } -------------------------------------------------------------------------------- /.gitignore: -------------------------------------------------------------------------------- 1 | ## Ignore Visual Studio temporary files, build results, and 2 | ## files generated by popular Visual Studio add-ons. 3 | ## 4 | ## Get latest from https://github.com/github/gitignore/blob/master/VisualStudio.gitignore 5 | 6 | # User-specific files 7 | *.suo 8 | *.user 9 | *.userosscache 10 | *.sln.docstates 11 | 12 | # User-specific files (MonoDevelop/Xamarin Studio) 13 | *.userprefs 14 | 15 | # Build results 16 | [Dd]ebug/ 17 | [Dd]ebugPublic/ 18 | [Rr]elease/ 19 | [Rr]eleases/ 20 | x64/ 21 | x86/ 22 | bld/ 23 | [Bb]in/ 24 | [Oo]bj/ 25 | [Ll]og/ 26 | 27 | # Visual Studio 2015 cache/options directory 28 | .vs/ 29 | # Uncomment if you have tasks that create the project's static files in wwwroot 30 | #wwwroot/ 31 | 32 | # MSTest test Results 33 | [Tt]est[Rr]esult*/ 34 | [Bb]uild[Ll]og.* 35 | 36 | # NUNIT 37 | *.VisualState.xml 38 | TestResult.xml 39 | 40 | # Build Results of an ATL Project 41 | [Dd]ebugPS/ 42 | [Rr]eleasePS/ 43 | dlldata.c 44 | 45 | # .NET Core 46 | project.lock.json 47 | project.fragment.lock.json 48 | artifacts/ 49 | **/Properties/launchSettings.json 50 | 51 | *_i.c 52 | *_p.c 53 | *_i.h 54 | *.ilk 55 | *.meta 56 | *.obj 57 | *.pch 58 | *.pdb 59 | *.pgc 60 | *.pgd 61 | *.rsp 62 | *.sbr 63 | *.tlb 64 | *.tli 65 | *.tlh 66 | *.tmp 67 | *.tmp_proj 68 | *.log 69 | *.vspscc 70 | *.vssscc 71 | .builds 72 | *.pidb 73 | *.svclog 74 | *.scc 75 | 76 | # Chutzpah Test files 77 | _Chutzpah* 78 | 79 | # Visual C++ cache files 80 | ipch/ 81 | *.aps 82 | *.ncb 83 | *.opendb 84 | *.opensdf 85 | *.sdf 86 | *.cachefile 87 | *.VC.db 88 | *.VC.VC.opendb 89 | 90 | # Visual Studio profiler 91 | *.psess 92 | *.vsp 93 | *.vspx 94 | *.sap 95 | 96 | # TFS 2012 Local Workspace 97 | $tf/ 98 | 99 | # Guidance Automation Toolkit 100 | *.gpState 101 | 102 | # ReSharper is a .NET coding add-in 103 | _ReSharper*/ 104 | *.[Rr]e[Ss]harper 105 | *.DotSettings.user 106 | 107 | # JustCode is a .NET coding add-in 108 | .JustCode 109 | 110 | # TeamCity is a build add-in 111 | _TeamCity* 112 | 113 | # DotCover is a Code Coverage Tool 114 | *.dotCover 115 | 116 | # Visual Studio code coverage results 117 | *.coverage 118 | *.coveragexml 119 | 120 | # NCrunch 121 | _NCrunch_* 122 | .*crunch*.local.xml 123 | nCrunchTemp_* 124 | 125 | # MightyMoose 126 | *.mm.* 127 | AutoTest.Net/ 128 | 129 | # Web workbench (sass) 130 | .sass-cache/ 131 | 132 | # Installshield output folder 133 | [Ee]xpress/ 134 | 135 | # DocProject is a documentation generator add-in 136 | DocProject/buildhelp/ 137 | DocProject/Help/*.HxT 138 | DocProject/Help/*.HxC 139 | DocProject/Help/*.hhc 140 | DocProject/Help/*.hhk 141 | DocProject/Help/*.hhp 142 | DocProject/Help/Html2 143 | DocProject/Help/html 144 | 145 | # Click-Once directory 146 | publish/ 147 | 148 | # Publish Web Output 149 | *.[Pp]ublish.xml 150 | *.azurePubxml 151 | # TODO: Comment the next line if you want to checkin your web deploy settings 152 | # but database connection strings (with potential passwords) will be unencrypted 153 | *.pubxml 154 | *.publishproj 155 | 156 | # Microsoft Azure Web App publish settings. Comment the next line if you want to 157 | # checkin your Azure Web App publish settings, but sensitive information contained 158 | # in these scripts will be unencrypted 159 | PublishScripts/ 160 | 161 | # NuGet Packages 162 | *.nupkg 163 | # The packages folder can be ignored because of Package Restore 164 | **/packages/* 165 | # except build/, which is used as an MSBuild target. 166 | !**/packages/build/ 167 | # Uncomment if necessary however generally it will be regenerated when needed 168 | #!**/packages/repositories.config 169 | # NuGet v3's project.json files produces more ignorable files 170 | *.nuget.props 171 | *.nuget.targets 172 | 173 | # Microsoft Azure Build Output 174 | csx/ 175 | *.build.csdef 176 | 177 | # Microsoft Azure Emulator 178 | ecf/ 179 | rcf/ 180 | 181 | # Windows Store app package directories and files 182 | AppPackages/ 183 | BundleArtifacts/ 184 | Package.StoreAssociation.xml 185 | _pkginfo.txt 186 | 187 | # Visual Studio cache files 188 | # files ending in .cache can be ignored 189 | *.[Cc]ache 190 | # but keep track of directories ending in .cache 191 | !*.[Cc]ache/ 192 | 193 | # Others 194 | ClientBin/ 195 | ~$* 196 | *~ 197 | *.dbmdl 198 | *.dbproj.schemaview 199 | *.jfm 200 | *.pfx 201 | *.publishsettings 202 | orleans.codegen.cs 203 | 204 | # Since there are multiple workflows, uncomment next line to ignore bower_components 205 | # (https://github.com/github/gitignore/pull/1529#issuecomment-104372622) 206 | #bower_components/ 207 | 208 | # RIA/Silverlight projects 209 | Generated_Code/ 210 | 211 | # Backup & report files from converting an old project file 212 | # to a newer Visual Studio version. Backup files are not needed, 213 | # because we have git ;-) 214 | _UpgradeReport_Files/ 215 | Backup*/ 216 | UpgradeLog*.XML 217 | UpgradeLog*.htm 218 | 219 | # SQL Server files 220 | *.mdf 221 | *.ldf 222 | *.ndf 223 | 224 | # Business Intelligence projects 225 | *.rdl.data 226 | *.bim.layout 227 | *.bim_*.settings 228 | 229 | # Microsoft Fakes 230 | FakesAssemblies/ 231 | 232 | # GhostDoc plugin setting file 233 | *.GhostDoc.xml 234 | 235 | # Node.js Tools for Visual Studio 236 | .ntvs_analysis.dat 237 | node_modules/ 238 | 239 | # Typescript v1 declaration files 240 | typings/ 241 | 242 | # Visual Studio 6 build log 243 | *.plg 244 | 245 | # Visual Studio 6 workspace options file 246 | *.opt 247 | 248 | # Visual Studio 6 auto-generated workspace file (contains which files were open etc.) 249 | *.vbw 250 | 251 | # Visual Studio LightSwitch build output 252 | **/*.HTMLClient/GeneratedArtifacts 253 | **/*.DesktopClient/GeneratedArtifacts 254 | **/*.DesktopClient/ModelManifest.xml 255 | **/*.Server/GeneratedArtifacts 256 | **/*.Server/ModelManifest.xml 257 | _Pvt_Extensions 258 | 259 | # Paket dependency manager 260 | .paket/paket.exe 261 | paket-files/ 262 | 263 | # FAKE - F# Make 264 | .fake/ 265 | 266 | # JetBrains Rider 267 | .idea/ 268 | *.sln.iml 269 | 270 | # CodeRush 271 | .cr/ 272 | 273 | # Python Tools for Visual Studio (PTVS) 274 | __pycache__/ 275 | *.pyc 276 | 277 | # Cake - Uncomment if you are using it 278 | # tools/** 279 | # !tools/packages.config 280 | 281 | # Telerik's JustMock configuration file 282 | *.jmconfig 283 | 284 | # BizTalk build output 285 | *.btp.cs 286 | *.btm.cs 287 | *.odx.cs 288 | *.xsd.cs 289 | -------------------------------------------------------------------------------- /src/DeepCopy/CopierGenerator.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | using System.Collections.Concurrent; 3 | using System.Reflection; 4 | using System.Reflection.Emit; 5 | using System.Runtime.CompilerServices; 6 | // ReSharper disable StaticMemberInGenericType 7 | 8 | namespace DeepCopy 9 | { 10 | /// 11 | /// Generates copy delegates. 12 | /// 13 | internal static class CopierGenerator 14 | { 15 | private static readonly ConcurrentDictionary> Copiers = new ConcurrentDictionary>(); 16 | private static readonly Type GenericType = typeof(T); 17 | private static readonly DeepCopyDelegate MatchingTypeCopier = CreateCopier(GenericType); 18 | private static readonly Func> GenerateCopier = CreateCopier; 19 | 20 | public static T Copy(T original, CopyContext context) 21 | { 22 | // ReSharper disable once ExpressionIsAlwaysNull 23 | if (original == null) return original; 24 | 25 | var type = original.GetType(); 26 | if (type == GenericType) return MatchingTypeCopier(original, context); 27 | 28 | var result = Copiers.GetOrAdd(type, GenerateCopier); 29 | return result(original, context); 30 | } 31 | 32 | /// 33 | /// Gets a copier for the provided type. 34 | /// 35 | /// The type. 36 | /// A copier for the provided type. 37 | private static DeepCopyDelegate CreateCopier(Type type) 38 | { 39 | if (type.IsArray) 40 | { 41 | return CreateArrayCopier(type); 42 | } 43 | 44 | if (DeepCopier.CopyPolicy.IsImmutable(type)) return (original, context) => original; 45 | 46 | // By-ref types are not supported. 47 | if (type.IsByRef) return ThrowNotSupportedType(type); 48 | 49 | var dynamicMethod = new DynamicMethod( 50 | type.Name + "DeepCopier", 51 | typeof(T), 52 | new[] {typeof(T), typeof(CopyContext)}, 53 | typeof(DeepCopier).Module, 54 | true); 55 | 56 | var il = dynamicMethod.GetILGenerator(); 57 | 58 | // Declare a variable to store the result. 59 | il.DeclareLocal(type); 60 | 61 | var needsTracking = DeepCopier.CopyPolicy.NeedsTracking(type); 62 | var hasCopyLabel = il.DefineLabel(); 63 | if (needsTracking) 64 | { 65 | // C#: if (context.TryGetCopy(original, out object existingCopy)) return (T)existingCopy; 66 | il.DeclareLocal(typeof(object)); 67 | il.Emit(OpCodes.Ldarg_1); 68 | il.Emit(OpCodes.Ldarg_0); 69 | il.Emit(OpCodes.Ldloca_S, (byte) 1); 70 | il.Emit(OpCodes.Call, DeepCopier.MethodInfos.TryGetCopy); 71 | il.Emit(OpCodes.Brtrue, hasCopyLabel); 72 | } 73 | 74 | // Construct the result. 75 | var constructorInfo = type.GetConstructor(Type.EmptyTypes); 76 | if (type.IsValueType) 77 | { 78 | // Value types can be initialized directly. 79 | il.Emit(OpCodes.Ldloca_S, (byte)0); 80 | il.Emit(OpCodes.Initobj, type); 81 | } 82 | else if (constructorInfo != null) 83 | { 84 | // If a default constructor exists, use that. 85 | il.Emit(OpCodes.Newobj, constructorInfo); 86 | il.Emit(OpCodes.Stloc_0); 87 | } 88 | else 89 | { 90 | // If no default constructor exists, create an instance using GetUninitializedObject 91 | il.Emit(OpCodes.Ldtoken, type); 92 | il.Emit(OpCodes.Call, DeepCopier.MethodInfos.GetTypeFromHandle); 93 | il.Emit(OpCodes.Call, DeepCopier.MethodInfos.GetUninitializedObject); 94 | il.Emit(OpCodes.Castclass, type); 95 | il.Emit(OpCodes.Stloc_0); 96 | } 97 | 98 | // An instance of a value types can never appear multiple times in an object graph, 99 | // so only record reference types in the context. 100 | if (needsTracking) 101 | { 102 | // Record the object. 103 | il.Emit(OpCodes.Ldarg_1); 104 | il.Emit(OpCodes.Ldarg_0); 105 | il.Emit(OpCodes.Ldloc_0); 106 | il.Emit(OpCodes.Call, DeepCopier.MethodInfos.RecordObject); 107 | } 108 | 109 | // Copy each field. 110 | foreach (var field in DeepCopier.CopyPolicy.GetCopyableFields(type)) 111 | { 112 | // Load a reference to the result. 113 | if (type.IsValueType) 114 | { 115 | // Value types need to be loaded by address rather than copied onto the stack. 116 | il.Emit(OpCodes.Ldloca_S, (byte)0); 117 | } 118 | else 119 | { 120 | il.Emit(OpCodes.Ldloc_0); 121 | } 122 | 123 | // Load the field from the result. 124 | il.Emit(OpCodes.Ldarg_0); 125 | il.Emit(OpCodes.Ldfld, field); 126 | 127 | // Deep-copy the field if needed, otherwise just leave it as-is. 128 | if (!DeepCopier.CopyPolicy.IsImmutable(field.FieldType)) 129 | { 130 | // Copy the field using the generic DeepCopy.Copy method. 131 | il.Emit(OpCodes.Ldarg_1); 132 | il.Emit(OpCodes.Call, DeepCopier.MethodInfos.CopyInner.MakeGenericMethod(field.FieldType)); 133 | } 134 | 135 | // Store the copy of the field on the result. 136 | il.Emit(OpCodes.Stfld, field); 137 | } 138 | 139 | // Return the result. 140 | il.Emit(OpCodes.Ldloc_0); 141 | il.Emit(OpCodes.Ret); 142 | 143 | if (needsTracking) 144 | { 145 | // only non-ValueType needsTracking 146 | il.MarkLabel(hasCopyLabel); 147 | il.Emit(OpCodes.Ldloc_1); 148 | il.Emit(OpCodes.Castclass, type); 149 | il.Emit(OpCodes.Ret); 150 | } 151 | 152 | return dynamicMethod.CreateDelegate(typeof(DeepCopyDelegate)) as DeepCopyDelegate; 153 | } 154 | 155 | private static DeepCopyDelegate CreateArrayCopier(Type type) 156 | { 157 | var elementType = type.GetElementType(); 158 | 159 | var rank = type.GetArrayRank(); 160 | var isImmutable = DeepCopier.CopyPolicy.IsImmutable(elementType); 161 | MethodInfo methodInfo; 162 | switch (rank) 163 | { 164 | case 1: 165 | if (isImmutable) 166 | { 167 | methodInfo = DeepCopier.MethodInfos.CopyArrayRank1Shallow; 168 | } 169 | else 170 | { 171 | methodInfo = DeepCopier.MethodInfos.CopyArrayRank1; 172 | } 173 | break; 174 | case 2: 175 | if (isImmutable) 176 | { 177 | methodInfo = DeepCopier.MethodInfos.CopyArrayRank2Shallow; 178 | } 179 | else 180 | { 181 | methodInfo = DeepCopier.MethodInfos.CopyArrayRank2; 182 | } 183 | break; 184 | default: 185 | return ArrayCopier.CopyArray; 186 | } 187 | 188 | return (DeepCopyDelegate) methodInfo.MakeGenericMethod(elementType).CreateDelegate(typeof(DeepCopyDelegate)); 189 | } 190 | 191 | [MethodImpl(MethodImplOptions.NoInlining)] 192 | private static DeepCopyDelegate ThrowNotSupportedType(Type type) 193 | { 194 | throw new NotSupportedException($"Unable to copy object of type {type}."); 195 | } 196 | } 197 | } 198 | -------------------------------------------------------------------------------- /src/DeepCopy/CopyPolicy.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | using System.Collections; 3 | using System.Collections.Concurrent; 4 | using System.Collections.Generic; 5 | using System.Linq; 6 | using System.Reflection; 7 | using System.Runtime.InteropServices.ComTypes; 8 | 9 | namespace DeepCopy 10 | { 11 | /// 12 | /// Methods for determining the copyability of types and fields. 13 | /// 14 | internal sealed class CopyPolicy 15 | { 16 | private enum Policy 17 | { 18 | Tracking, 19 | Mutable, 20 | Immutable 21 | } 22 | 23 | private readonly ConcurrentDictionary policies = new ConcurrentDictionary(); 24 | private readonly RuntimeTypeHandle intPtrTypeHandle = typeof(IntPtr).TypeHandle; 25 | private readonly RuntimeTypeHandle uIntPtrTypeHandle = typeof(UIntPtr).TypeHandle; 26 | private readonly Type delegateType = typeof(Delegate); 27 | 28 | public CopyPolicy() 29 | { 30 | this.policies[typeof(object)] = Policy.Tracking; // we need to track 31 | } 32 | 33 | /// 34 | /// Returns a sorted list of the copyable fields of the provided type. 35 | /// 36 | /// The type. 37 | /// A sorted list of the fields of the provided type. 38 | public List GetCopyableFields(Type type) 39 | { 40 | var result = 41 | GetAllFields(type) 42 | .Where(field => IsSupportedFieldType(field.FieldType)) 43 | .ToList(); 44 | result.Sort(FieldInfoComparer.Instance); 45 | return result; 46 | 47 | IEnumerable GetAllFields(Type containingType) 48 | { 49 | const BindingFlags allFields = 50 | BindingFlags.Instance | BindingFlags.Public | BindingFlags.NonPublic | BindingFlags.DeclaredOnly; 51 | var current = containingType; 52 | while (current != typeof(object) && current != null) 53 | { 54 | var fields = current.GetFields(allFields); 55 | foreach (var field in fields) 56 | { 57 | yield return field; 58 | } 59 | 60 | current = current.BaseType; 61 | } 62 | } 63 | 64 | bool IsSupportedFieldType(Type fieldType) 65 | { 66 | if (fieldType.IsPointer || fieldType.IsByRef) return false; 67 | 68 | var handle = fieldType.TypeHandle; 69 | if (handle.Equals(this.intPtrTypeHandle)) return false; 70 | if (handle.Equals(this.uIntPtrTypeHandle)) return false; 71 | if (this.delegateType.IsAssignableFrom(fieldType)) return false; 72 | 73 | return true; 74 | } 75 | } 76 | 77 | /// 78 | /// Returns true if the provided type is immutable, otherwise false. 79 | /// 80 | /// The type. 81 | /// true if the provided type is immutable, otherwise false. 82 | public bool IsImmutable(Type type) 83 | { 84 | return this.GetPolicy(type) == Policy.Immutable; 85 | } 86 | 87 | public bool NeedsTracking(Type type) 88 | { 89 | if (type.IsValueType) 90 | { 91 | return false; 92 | } 93 | var policy = GetPolicy(type); 94 | // we found something mutable now we need to check if it needs tracking 95 | if (policy == Policy.Mutable) 96 | { 97 | var copyableFields = GetCopyableFields(type); 98 | var queue = new Queue(copyableFields); 99 | var duplicateCheck = new HashSet(AssignableFromEqualityComparer.Instance) {type}; // add root 100 | while (queue.Count > 0) 101 | { 102 | var current = queue.Dequeue(); 103 | var fieldType = current.FieldType; 104 | var fieldPolicy = GetPolicy(fieldType); 105 | if(fieldPolicy == Policy.Immutable) 106 | { 107 | continue; 108 | } 109 | if (fieldPolicy == Policy.Tracking) 110 | { 111 | this.policies[type] = Policy.Tracking; 112 | return true; 113 | } 114 | if (duplicateCheck.Add(fieldType)) 115 | { 116 | if (typeof(IEnumerable).IsAssignableFrom(fieldType)) // Rule 5: enumerable mutable fields need tracking 117 | { 118 | this.policies[type] = Policy.Tracking; 119 | return true; 120 | } 121 | var fieldFields = GetCopyableFields(fieldType); 122 | foreach (var fieldField in fieldFields) 123 | { 124 | queue.Enqueue(fieldField); // Rule 6: Recursive 125 | } 126 | } 127 | else 128 | { 129 | this.policies[type] = Policy.Tracking; // Rule 4: one or more mutable fields of the same type 130 | return true; 131 | } 132 | } 133 | 134 | } 135 | return this.GetPolicy(type) == Policy.Tracking; 136 | } 137 | 138 | private Policy GetPolicy(Type type) 139 | { 140 | if (this.policies.TryGetValue(type, out var result)) 141 | { 142 | return result; 143 | } 144 | 145 | if (type.GetCustomAttribute(false) != null) 146 | { 147 | return this.policies[type] = Policy.Immutable; 148 | } 149 | 150 | // Rule 1: primitives and quasi primitves 151 | if (type.IsPrimitive || type.IsEnum || type.IsPointer || type == typeof(string)) 152 | { 153 | return this.policies[type] = Policy.Immutable; 154 | } 155 | 156 | // covers interface and abstract type too 157 | if (!type.IsSealed) 158 | { 159 | return this.policies[type] = Policy.Mutable; 160 | } 161 | 162 | if (type.IsArray) 163 | { 164 | return this.policies[type] = Policy.Mutable; 165 | } 166 | // Rule 1,2 167 | if (type.IsValueType) 168 | { 169 | var copyableFields = GetCopyableFields(type); 170 | foreach (var copyableField in copyableFields) 171 | { 172 | var fieldType = copyableField.FieldType; 173 | if (GetPolicy(fieldType) != Policy.Immutable) 174 | { 175 | return this.policies[type] = Policy.Mutable; 176 | } 177 | } 178 | } 179 | // Rule 3 180 | else if (type.IsClass) 181 | { 182 | var copyableFields = GetCopyableFields(type); 183 | foreach (var copyableField in copyableFields) 184 | { 185 | if (!copyableField.IsInitOnly) 186 | { 187 | return this.policies[type] = Policy.Mutable; 188 | } 189 | 190 | var fieldType = copyableField.FieldType; 191 | if (GetPolicy(fieldType) != Policy.Immutable) 192 | { 193 | return this.policies[type] = Policy.Mutable; 194 | } 195 | } 196 | } 197 | 198 | return this.policies[type] = Policy.Immutable; 199 | } 200 | 201 | /// 202 | /// A comparer for which compares by name. 203 | /// 204 | private sealed class FieldInfoComparer : IComparer 205 | { 206 | /// 207 | /// Gets the singleton instance of this class. 208 | /// 209 | public static FieldInfoComparer Instance { get; } = new FieldInfoComparer(); 210 | 211 | /// 212 | public int Compare(FieldInfo x, FieldInfo y) 213 | { 214 | return string.Compare(x.Name, y.Name, StringComparison.Ordinal); 215 | } 216 | } 217 | 218 | private sealed class AssignableFromEqualityComparer : IEqualityComparer 219 | { 220 | public static AssignableFromEqualityComparer Instance { get; } = new AssignableFromEqualityComparer(); 221 | private static readonly Type ObjectType = typeof(object); 222 | public bool Equals(Type x, Type y) 223 | { 224 | // We can't reason about object 225 | if (x == ObjectType || y == ObjectType) 226 | { 227 | return false; 228 | } 229 | return x.IsAssignableFrom(y) || y.IsAssignableFrom(x); 230 | } 231 | 232 | public int GetHashCode(Type obj) 233 | { 234 | return 0; 235 | } 236 | } 237 | 238 | } 239 | } 240 | -------------------------------------------------------------------------------- /test/DeepCopy.UnitTests/CopyTests.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | using System.Collections; 3 | using System.Collections.Generic; 4 | using Xunit; 5 | 6 | namespace DeepCopy.UnitTests 7 | { 8 | [Trait("TestCategory", "BVT")] 9 | public class CopyTests 10 | { 11 | [Fact] 12 | public void CanCopyStrings() 13 | { 14 | var original = "hello!"; 15 | var result = DeepCopier.Copy(original); 16 | Assert.Same(original, result); 17 | } 18 | 19 | [Fact] 20 | public void CanCopyIntegers() 21 | { 22 | var original = 123; 23 | var result = DeepCopier.Copy(original); 24 | Assert.Equal(original, result); 25 | } 26 | 27 | [Fact] 28 | public void CanCopyArrays() 29 | { 30 | var original = new object[] { 123, "hello!" }; 31 | var result = DeepCopier.Copy(original); 32 | Assert.Equal(original, result); 33 | Assert.NotSame(original, result); 34 | } 35 | 36 | [Fact] 37 | public void CanCopyArraysTwoDimensional() 38 | { 39 | var original = new object[][] {new object[] {123, "hello!"}, new object[] {123, "hello!"}}; 40 | var result = DeepCopier.Copy(original); 41 | Assert.Equal(original, result); 42 | Assert.NotSame(original, result); 43 | } 44 | 45 | [Fact] 46 | public void CanCopyArraysRank3() 47 | { 48 | var original = new object[,,] { { { "Hello", 2, "World" }, { 300.0f, "World", 33 } }, { { 92, 5.0m, 135 }, { 30, true, 3 } } }; 49 | var result = DeepCopier.Copy(original); 50 | Assert.Equal(original, result); 51 | Assert.NotSame(original, result); 52 | } 53 | 54 | [Fact] 55 | public void CanCopyArraysRank3WithImmutable() 56 | { 57 | var immutable = new ImmutablePoco(); 58 | var original = new object[,,] 59 | { 60 | { 61 | {"Hello", 2, immutable}, 62 | {300.0f, immutable, 33} 63 | }, 64 | { 65 | {immutable, 5.0m, 135}, 66 | {30, immutable, 3} 67 | } 68 | }; 69 | var result = DeepCopier.Copy(original); 70 | Assert.Equal(original, result); 71 | Assert.NotSame(original, result); 72 | Assert.Same(immutable, result[0, 0, 2]); 73 | Assert.Same(immutable, result[0, 1, 1]); 74 | Assert.Same(immutable, result[1, 0, 0]); 75 | Assert.Same(immutable, result[1, 1, 1]); 76 | } 77 | 78 | [Fact] 79 | public void CanCopyThreeDimensionalArrays() 80 | { 81 | var original = new object[][][] 82 | { 83 | new object[][] 84 | { 85 | new object[] {123, "hello!"} 86 | }, 87 | new object[][] 88 | { 89 | new object[] {123, "hello!", "world"}, 90 | new object[] {123, "hello!"} 91 | }, 92 | }; 93 | var result = DeepCopier.Copy(original); 94 | Assert.Equal(original, result); 95 | Assert.NotSame(original, result); 96 | } 97 | 98 | [Fact] 99 | public void CanCopyJaggedMultidimensionalArrays() 100 | { 101 | var original = new object[3][,] 102 | { 103 | new object[,] {{123, "hello!"}, {5, 7}}, 104 | new object[,] {{456, "world"}, {4, 6}, {"hello", "world"}}, 105 | new object[,] {{789, "universe"}, {99, 88}, {0, 9}} 106 | }; 107 | 108 | var result = DeepCopier.Copy(original); 109 | Assert.Equal(original, result); 110 | Assert.NotSame(original, result); 111 | } 112 | 113 | [Fact] 114 | public void CanCopyInterfaceField() 115 | { 116 | PocoWithInterface original = new PocoWithInterface(); 117 | 118 | original.Collection.Add("A"); 119 | 120 | var result = DeepCopier.Copy(original); 121 | 122 | Assert.NotSame(original, result); 123 | Assert.NotSame(original.Collection, result.Collection); 124 | } 125 | 126 | [Fact] 127 | public void CanCopyAbstractBaseClassField() 128 | { 129 | PocoWithAbstractBaseClass original = 130 | new PocoWithAbstractBaseClass(new DerivedClass()); 131 | 132 | 133 | var result = DeepCopier.Copy(original); 134 | 135 | Assert.NotSame(original, result); 136 | Assert.NotSame(original.Child, result.Child); 137 | } 138 | 139 | [Fact] 140 | public void CanCopyBaseClassField() 141 | { 142 | PocoWithBaseClass original = 143 | new PocoWithBaseClass(new DerivedChildClass()); 144 | 145 | 146 | var result = DeepCopier.Copy(original); 147 | 148 | Assert.NotSame(original, result); 149 | Assert.NotSame(original.Child, result.Child); 150 | } 151 | 152 | [Fact] 153 | public void CanCopyCollections() 154 | { 155 | { 156 | var original = new HashSet(new[] { 123, 4, 5, 6 }); 157 | var result = DeepCopier.Copy(original); 158 | Assert.Equal(original, result); 159 | Assert.NotSame(original, result); 160 | } 161 | { 162 | var original = new Dictionary {[1] = 1, [2] = 2}; 163 | var result = DeepCopier.Copy(original); 164 | Assert.Equal(original, result); 165 | Assert.NotSame(original, result); 166 | } 167 | } 168 | 169 | [Fact] 170 | public void CanCopyPrimitiveArrays() 171 | { 172 | var original = new int[] { 1, 2, 3 }; 173 | var result = DeepCopier.Copy(original); 174 | Assert.Equal(original, result); 175 | Assert.NotSame(original, result); 176 | } 177 | 178 | [Fact] 179 | public void CanCopyPrimitiveArraysRank3() 180 | { 181 | var original = new int[,,] { { { 12, 2, 35 }, { 300, 78, 33 } }, { { 92, 42, 135 }, { 30, 7, 3 } } }; 182 | var result = DeepCopier.Copy(original); 183 | Assert.Equal(original, result); 184 | Assert.NotSame(original, result); 185 | } 186 | 187 | [Fact] 188 | public void CanCopyPrimitiveTwoDimensionalArraysShallow() 189 | { 190 | var original = new int[][] 191 | { 192 | new int[] {1,3,5,7,9}, 193 | new int[] {0,2,4,6}, 194 | new int[] {11,22} 195 | }; 196 | var result = DeepCopier.Copy(original); 197 | Assert.Equal(original, result); 198 | Assert.NotSame(original, result); 199 | } 200 | 201 | [Fact] 202 | public void CanCopyPrimitiveThreeDimensionalArrays() 203 | { 204 | var original = new int[][][] 205 | { 206 | new int[][] 207 | { 208 | new int[] {1, 2} 209 | }, 210 | new int[][] 211 | { 212 | new int[] {1, 3, 5, 7, 9}, 213 | new int[] {11, 22} 214 | }, 215 | }; 216 | var result = DeepCopier.Copy(original); 217 | Assert.Equal(original, result); 218 | Assert.NotSame(original, result); 219 | } 220 | 221 | [Fact] 222 | public void CanCopyPrimitiveJaggedMultidimensionalArrays() 223 | { 224 | var original = new int[3][,] 225 | { 226 | new int[,] { {1,3}, {5,7} }, 227 | new int[,] { {0,2}, {4,6}, {8,10} }, 228 | new int[,] { {11,22}, {99,88}, {0,9} } 229 | }; 230 | 231 | var result = DeepCopier.Copy(original); 232 | Assert.Equal(original, result); 233 | Assert.NotSame(original, result); 234 | } 235 | 236 | [Fact] 237 | public void ImmutableTypesAreNotCopied() 238 | { 239 | var original = new ImmutablePoco { Reference = new object[] { 123, "hello!" } }; 240 | var result = DeepCopier.Copy(original); 241 | Assert.Same(original.Reference, result.Reference); 242 | Assert.Same(original, result); 243 | } 244 | 245 | [Fact] 246 | public void ImmutableWrapperTypesAreNotCopied() 247 | { 248 | var original = Immutable.Create(new object[] { 123, "hello!" }); 249 | var result = DeepCopier.Copy(original); 250 | Assert.Same(original.Value, result.Value); 251 | } 252 | 253 | [Fact] 254 | public void CanCopyCyclicObjects() 255 | { 256 | var original = new Poco(); 257 | original.Reference = original; 258 | 259 | var result = DeepCopier.Copy(original); 260 | Assert.NotSame(original, result); 261 | Assert.Same(result, result.Reference); 262 | } 263 | 264 | [Fact] 265 | public void ReferencesInArraysArePreserved() 266 | { 267 | var poco = new Poco(); 268 | var original = new[] { poco, poco }; 269 | 270 | var result = DeepCopier.Copy(original); 271 | Assert.NotSame(original, result); 272 | Assert.Same(result[0], result[1]); 273 | } 274 | 275 | [Fact] 276 | public void CanCopyPrivateReadonly() 277 | { 278 | var poco = new Poco(); 279 | poco.Reference = poco; 280 | var original = new PocoWithPrivateReadonly(poco); 281 | 282 | var result = DeepCopier.Copy(original); 283 | Assert.NotSame(original, result); 284 | Assert.Same(result.GetReference(), ((Poco)result.GetReference()).Reference); 285 | } 286 | 287 | [Fact] 288 | public void CanCopyMutableKeyValuePair() 289 | { 290 | var original = new KeyValuePair("Hello", new Poco()); 291 | var result = DeepCopier.Copy(original); 292 | Assert.Same(original.Key, result.Key); 293 | Assert.NotSame(original.Value, result.Value); 294 | } 295 | 296 | [Fact] 297 | public void CanCopyMutableValueTupleRest() 298 | { 299 | var original = new ValueTuple>(5, "hello", 300 | 1d, 2, "world", 3d, 7, 301 | new ValueTuple(new Poco())); 302 | var result = DeepCopier.Copy(original); 303 | Assert.NotEqual(original.Rest, result.Rest); 304 | } 305 | 306 | [Theory] 307 | [MemberData(nameof(ImmutableTestData))] 308 | public void CanCopyImmutables(object original) 309 | { 310 | var result = DeepCopier.Copy(original); 311 | Assert.Equal(result, original); 312 | } 313 | 314 | [Fact] 315 | public void CanCopyCyclicObjectsWithChildren() 316 | { 317 | var original = new CyclicPocoWithChildren(); 318 | original.Children.Add(original); 319 | 320 | var result = DeepCopier.Copy(original); 321 | Assert.NotSame(original, result); 322 | Assert.Same(result, result.Children[0]); 323 | } 324 | 325 | [Fact] 326 | public void CanCopyCyclicObjectsWithSibling() 327 | { 328 | var original = new CyclicPocoWithSibling(); 329 | original.Sibling = original; 330 | 331 | var result = DeepCopier.Copy(original); 332 | Assert.NotSame(original, result); 333 | Assert.Same(result, result.Sibling); 334 | } 335 | 336 | [Fact] 337 | public void CanCopyCyclicObjectsWithBaseSibling() 338 | { 339 | var original = new CyclicPocoWithBaseSibling(); 340 | original.BaseSibling = original; 341 | 342 | var result = DeepCopier.Copy(original); 343 | Assert.NotSame(original, result); 344 | Assert.Same(result, result.BaseSibling); 345 | } 346 | 347 | [Fact] 348 | public void RegressionTest_20() 349 | { 350 | var original = new Dictionary() { { 1, new Component() }, }; 351 | var result = DeepCopier.Copy(original); 352 | GC.GetTotalMemory(true); // force full GC 353 | } 354 | 355 | public static IEnumerable ImmutableTestData() 356 | { 357 | yield return new object[] { 5m }; 358 | yield return new object[] { DateTime.Now }; 359 | yield return new object[] { TimeSpan.MaxValue }; 360 | yield return new object[] { "Hello World" }; 361 | yield return new object[] { Guid.NewGuid() }; 362 | yield return new object[] { DateTimeOffset.Now }; 363 | yield return new object[] { new Version(1, 0, 0, 0) }; 364 | yield return new object[] { new Uri("http://localhost") }; 365 | yield return new object[] { new KeyValuePair("Hello", "World") }; 366 | yield return new object[] { new Tuple(5) }; 367 | yield return new object[] { new Tuple(5, "hello") }; 368 | yield return new object[] { new Tuple(5, "hello", 1d) }; 369 | yield return new object[] { new Tuple(5, "hello", 1d, 2) }; 370 | yield return new object[] { new Tuple(5, "hello", 1d, 2, "world") }; 371 | yield return new object[] 372 | {new Tuple(5, "hello", 1d, 2, "world", 3d)}; 373 | yield return new object[] 374 | {new Tuple(5, "hello", 1d, 2, "world", 3d, 7)}; 375 | yield return new object[] 376 | { 377 | new Tuple>(5, "hello", 1d, 2, "world", 3d, 7, 378 | Tuple.Create("universe")) 379 | }; 380 | yield return new object[] { new ValueTuple(5) }; 381 | yield return new object[] { new ValueTuple(5, "hello") }; 382 | yield return new object[] { new ValueTuple(5, "hello", 1d) }; 383 | yield return new object[] { new ValueTuple(5, "hello", 1d, 2) }; 384 | yield return new object[] { new ValueTuple(5, "hello", 1d, 2, "world") }; 385 | yield return new object[] 386 | {new ValueTuple(5, "hello", 1d, 2, "world", 3d)}; 387 | yield return new object[] 388 | {new ValueTuple(5, "hello", 1d, 2, "world", 3d, 7)}; 389 | yield return new object[] 390 | { 391 | new ValueTuple>(5, "hello", 1d, 2, "world", 3d, 7, 392 | new ValueTuple("universe")) 393 | }; 394 | } 395 | 396 | [Immutable] 397 | private class ImmutablePoco 398 | { 399 | public object Reference { get; set; } 400 | } 401 | 402 | private class Poco 403 | { 404 | public object Reference { get; set; } 405 | } 406 | 407 | private class PocoWithInterface 408 | { 409 | public readonly ICollection Collection = new List(); 410 | } 411 | 412 | private class PocoWithAbstractBaseClass 413 | { 414 | public PocoWithAbstractBaseClass(AbstractBaseClass child) 415 | { 416 | Child = child; 417 | } 418 | public readonly AbstractBaseClass Child; 419 | } 420 | 421 | 422 | private class PocoWithBaseClass 423 | { 424 | public PocoWithBaseClass(BaseClass child) 425 | { 426 | Child = child; 427 | } 428 | public readonly BaseClass Child; 429 | } 430 | 431 | private class PocoWithPrivateReadonly 432 | { 433 | private readonly object reference; 434 | public PocoWithPrivateReadonly(object reference) 435 | { 436 | this.reference = reference; 437 | } 438 | 439 | public object GetReference() => this.reference; 440 | } 441 | 442 | private class CyclicPocoWithChildren : CyclicPocoBaseSibling 443 | { 444 | public List Children { get; set; } = new List(); 445 | } 446 | 447 | private class CyclicPocoWithSibling : CyclicPocoBaseSibling 448 | { 449 | public CyclicPocoWithSibling Sibling { get; set; } 450 | } 451 | 452 | private class CyclicPocoWithBaseSibling : CyclicPocoBaseSibling 453 | { 454 | public CyclicPocoBaseSibling BaseSibling { get; set; } 455 | } 456 | 457 | private class CyclicPocoBaseSibling 458 | { 459 | public string Name { get; set; } 460 | } 461 | 462 | private abstract class AbstractBaseClass 463 | { 464 | public readonly string Name = "Hello World"; 465 | } 466 | 467 | private class DerivedClass : AbstractBaseClass 468 | { 469 | public string Value { get; set; } 470 | } 471 | 472 | private class BaseClass 473 | { 474 | public readonly string Name = "Hello World"; 475 | } 476 | 477 | private class DerivedChildClass : BaseClass 478 | { 479 | public string Value { get; set; } 480 | } 481 | 482 | private class Component 483 | { 484 | public Transform Transform; 485 | } 486 | 487 | private class Transform : Component 488 | { 489 | 490 | } 491 | } 492 | } 493 | --------------------------------------------------------------------------------