├── Directory.Build.props
├── Cloneable.Sample
├── Cloneable.Sample.csproj
├── SimpleClone.cs
├── SimpleCloneExplicit.cs
├── DeepClone.cs
├── SafeDeepClone.cs
└── Program.cs
├── .github
└── workflows
│ ├── build-pipeline.yml
│ └── publish.yml
├── Cloneable
├── SymbolExtensions.cs
├── Cloneable.csproj
├── SyntaxReceiver.cs
├── tools
│ ├── install.ps1
│ └── uninstall.ps1
└── CloneableGenerator.cs
├── README.md
├── Cloneable.sln
└── .gitignore
/Directory.Build.props:
--------------------------------------------------------------------------------
1 |
2 |
3 | 1.3.0.0
4 |
5 |
6 |
7 | ..\Output\$(Configuration)
8 |
9 |
10 |
--------------------------------------------------------------------------------
/Cloneable.Sample/Cloneable.Sample.csproj:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 | Exe
5 | net5.0
6 | latest
7 |
8 |
9 |
10 |
11 |
12 |
13 |
14 |
--------------------------------------------------------------------------------
/Cloneable.Sample/SimpleClone.cs:
--------------------------------------------------------------------------------
1 | using System;
2 |
3 | namespace Cloneable.Sample
4 | {
5 | [Cloneable]
6 | public partial class SimpleClone
7 | {
8 | public string A { get; set; }
9 |
10 | [IgnoreClone]
11 | public int B { get; set; }
12 |
13 | public override string ToString()
14 | {
15 | return $"{nameof(SimpleClone)}:{Environment.NewLine}" +
16 | $"\tA:\t{A}" +
17 | Environment.NewLine +
18 | $"\tB:\t{B}";
19 | }
20 | }
21 | }
22 |
--------------------------------------------------------------------------------
/Cloneable.Sample/SimpleCloneExplicit.cs:
--------------------------------------------------------------------------------
1 | using System;
2 |
3 | namespace Cloneable.Sample
4 | {
5 | [Cloneable(ExplicitDeclaration = true)]
6 | public partial class SimpleCloneExplicit
7 | {
8 | public string A { get; set; }
9 |
10 | [Clone]
11 | public int B { get; set; }
12 |
13 | public override string ToString()
14 | {
15 | return $"{nameof(SimpleCloneExplicit)}:{Environment.NewLine}" +
16 | $"\tA:\t{A}" +
17 | Environment.NewLine +
18 | $"\tB:\t{B}";
19 | }
20 | }
21 | }
22 |
--------------------------------------------------------------------------------
/Cloneable.Sample/DeepClone.cs:
--------------------------------------------------------------------------------
1 | using System;
2 |
3 | namespace Cloneable.Sample
4 | {
5 | [Cloneable]
6 | public partial class DeepClone
7 | {
8 | public string A { get; set; }
9 | public SimpleClone Simple { get; set; }
10 |
11 | public override string ToString()
12 | {
13 | return $"{nameof(DeepClone)}:{Environment.NewLine}" +
14 | $"\tA:\t{A}" +
15 | Environment.NewLine +
16 | $"\tSimple.A:\t{Simple?.A}" +
17 | Environment.NewLine +
18 | $"\tSimple.B:\t{Simple?.B}";
19 | }
20 | }
21 | }
22 |
--------------------------------------------------------------------------------
/.github/workflows/build-pipeline.yml:
--------------------------------------------------------------------------------
1 | name: .NET Core
2 |
3 | on:
4 | push:
5 | branches: [ master ]
6 | pull_request:
7 | branches: [ master ]
8 |
9 | jobs:
10 | build:
11 |
12 | runs-on: ubuntu-latest
13 |
14 | steps:
15 | - uses: actions/checkout@v2
16 | - name: Setup .NET Core
17 | uses: actions/setup-dotnet@v1
18 | with:
19 | dotnet-version: 5.0.100
20 | - name: Install dependencies
21 | run: dotnet restore
22 | - name: Build
23 | run: dotnet build --configuration Release --no-restore
24 | - name: Test
25 | run: dotnet test --no-restore --verbosity normal
26 |
--------------------------------------------------------------------------------
/Cloneable/SymbolExtensions.cs:
--------------------------------------------------------------------------------
1 | using Microsoft.CodeAnalysis;
2 | using System.Collections.Generic;
3 | using System.Linq;
4 |
5 | namespace Cloneable
6 | {
7 | internal static class SymbolExtensions
8 | {
9 | public static bool TryGetAttribute(this ISymbol symbol, INamedTypeSymbol attributeType, out IEnumerable attributes)
10 | {
11 | attributes = symbol.GetAttributes()
12 | .Where(a => SymbolEqualityComparer.Default.Equals(a.AttributeClass, attributeType));
13 | return attributes.Any();
14 | }
15 |
16 | public static bool HasAttribute(this ISymbol symbol, INamedTypeSymbol attributeType)
17 | {
18 | return symbol.GetAttributes().Any(a => SymbolEqualityComparer.Default.Equals(a.AttributeClass, attributeType));
19 | }
20 | }
21 | }
22 |
--------------------------------------------------------------------------------
/Cloneable.Sample/SafeDeepClone.cs:
--------------------------------------------------------------------------------
1 | using System;
2 |
3 | namespace Cloneable.Sample
4 | {
5 | [Cloneable]
6 | public partial class SafeDeepClone
7 | {
8 | public string A { get; set; }
9 | public SafeDeepCloneChild Child { get; set; }
10 |
11 | public override string ToString()
12 | {
13 | return $"{nameof(SafeDeepClone)}:{Environment.NewLine}" +
14 | $"\tA:\t{A}" +
15 | Environment.NewLine +
16 | $"\tChild.A:\t{Child?.A}";
17 | }
18 | }
19 |
20 | [Cloneable]
21 | public partial class SafeDeepCloneChild
22 | {
23 | public string A { get; set; }
24 | public SafeDeepClone Parent { get; set; }
25 |
26 | public override string ToString()
27 | {
28 | return $"{nameof(SafeDeepCloneChild)}:{Environment.NewLine}" +
29 | $"\tA:\t{A}" +
30 | Environment.NewLine +
31 | $"\tParent.A:\t{Parent?.A}";
32 | }
33 | }
34 | }
35 |
--------------------------------------------------------------------------------
/Cloneable/Cloneable.csproj:
--------------------------------------------------------------------------------
1 |
2 |
3 | MH Mostmand
4 | Auto-generator of Clone method using C# Source Generator
5 | netstandard2.0
6 | latest
7 | enable
8 | false
9 | https://github.com/mostmand/Cloneable
10 |
11 |
12 |
13 |
14 |
15 |
16 |
17 |
18 |
19 |
20 |
21 |
22 |
--------------------------------------------------------------------------------
/Cloneable/SyntaxReceiver.cs:
--------------------------------------------------------------------------------
1 | using Microsoft.CodeAnalysis;
2 | using Microsoft.CodeAnalysis.CSharp.Syntax;
3 | using System.Collections.Generic;
4 | ///
5 | /// Created on demand before each generation pass
6 | ///
7 | namespace Cloneable
8 | {
9 | internal class SyntaxReceiver : ISyntaxReceiver
10 | {
11 | public IList CandidateClasses { get; } = new List();
12 |
13 | ///
14 | /// Called for every syntax node in the compilation, we can inspect the nodes and save any information useful for generation
15 | ///
16 | public void OnVisitSyntaxNode(SyntaxNode syntaxNode)
17 | {
18 | // any field with at least one attribute is a candidate for being cloneable
19 | if (syntaxNode is ClassDeclarationSyntax classDeclarationSyntax &&
20 | classDeclarationSyntax.AttributeLists.Count > 0)
21 | {
22 | CandidateClasses.Add(classDeclarationSyntax);
23 | }
24 | }
25 | }
26 | }
--------------------------------------------------------------------------------
/README.md:
--------------------------------------------------------------------------------
1 | # Cloneable
2 | Auto generate Clone method using C# Source Generator
3 |
4 | There are times you want to make a clone of an object. You can implement a clone method, but when a developer adds a new Field or Property the clone method should be changed too. Another way is to use reflection which is not performant.
5 | This source generator saves your time by generating the boilerplate code for cloning an object.
6 |
7 | ### Installing Cloneable
8 | You should install [Cloneable with NuGet](https://www.nuget.org/packages/Cloneable):
9 |
10 | Install-Package Cloneable
11 |
12 | Or via the .NET Core command line interface:
13 |
14 | dotnet add package Cloneable
15 |
16 | Either commands, from Package Manager Console or .NET Core CLI, will download and install Cloneable and all required dependencies.
17 |
18 | ### Usage
19 |
20 | You can add clone method to a class by making it partial and adding the attribute `Cloneable` on top of it. An example is provided in Cloneable.Sample project.
21 |
22 | Source generators are introduced in dotnet 5.0. So make sure to have Visual Studio 16.8 or dotnet 5.0 sdk installed.
23 |
24 | Here is a simple example:
25 |
26 | ```csharp
27 | [Cloneable]
28 | public partial class Foo
29 | {
30 | public string A { get; set; }
31 | public int B { get; set; }
32 | }
33 | ```
34 |
35 | For more examples please visit the [sample project](https://github.com/mostmand/Cloneable/tree/master/Cloneable.Sample).
36 |
--------------------------------------------------------------------------------
/Cloneable/tools/install.ps1:
--------------------------------------------------------------------------------
1 | param($installPath, $toolsPath, $package, $project)
2 |
3 | $analyzersPaths = Join-Path (Join-Path (Split-Path -Path $toolsPath -Parent) "analyzers" ) * -Resolve
4 |
5 | foreach($analyzersPath in $analyzersPaths)
6 | {
7 | # Install the language agnostic analyzers.
8 | if (Test-Path $analyzersPath)
9 | {
10 | foreach ($analyzerFilePath in Get-ChildItem $analyzersPath -Filter *.dll)
11 | {
12 | if($project.Object.AnalyzerReferences)
13 | {
14 | $project.Object.AnalyzerReferences.Add($analyzerFilePath.FullName)
15 | }
16 | }
17 | }
18 | }
19 |
20 | # $project.Type gives the language name like (C# or VB.NET)
21 | $languageFolder = ""
22 | if($project.Type -eq "C#")
23 | {
24 | $languageFolder = "cs"
25 | }
26 | if($project.Type -eq "VB.NET")
27 | {
28 | $languageFolder = "vb"
29 | }
30 | if($languageFolder -eq "")
31 | {
32 | return
33 | }
34 |
35 | foreach($analyzersPath in $analyzersPaths)
36 | {
37 | # Install language specific analyzers.
38 | $languageAnalyzersPath = join-path $analyzersPath $languageFolder
39 | if (Test-Path $languageAnalyzersPath)
40 | {
41 | foreach ($analyzerFilePath in Get-ChildItem $languageAnalyzersPath -Filter *.dll)
42 | {
43 | if($project.Object.AnalyzerReferences)
44 | {
45 | $project.Object.AnalyzerReferences.Add($analyzerFilePath.FullName)
46 | }
47 | }
48 | }
49 | }
--------------------------------------------------------------------------------
/Cloneable/tools/uninstall.ps1:
--------------------------------------------------------------------------------
1 | param($installPath, $toolsPath, $package, $project)
2 |
3 | $analyzersPaths = Join-Path (Join-Path (Split-Path -Path $toolsPath -Parent) "analyzers" ) * -Resolve
4 |
5 | foreach($analyzersPath in $analyzersPaths)
6 | {
7 | # Uninstall the language agnostic analyzers.
8 | if (Test-Path $analyzersPath)
9 | {
10 | foreach ($analyzerFilePath in Get-ChildItem $analyzersPath -Filter *.dll)
11 | {
12 | if($project.Object.AnalyzerReferences)
13 | {
14 | $project.Object.AnalyzerReferences.Remove($analyzerFilePath.FullName)
15 | }
16 | }
17 | }
18 | }
19 |
20 | # $project.Type gives the language name like (C# or VB.NET)
21 | $languageFolder = ""
22 | if($project.Type -eq "C#")
23 | {
24 | $languageFolder = "cs"
25 | }
26 | if($project.Type -eq "VB.NET")
27 | {
28 | $languageFolder = "vb"
29 | }
30 | if($languageFolder -eq "")
31 | {
32 | return
33 | }
34 |
35 | foreach($analyzersPath in $analyzersPaths)
36 | {
37 | # Uninstall language specific analyzers.
38 | $languageAnalyzersPath = join-path $analyzersPath $languageFolder
39 | if (Test-Path $languageAnalyzersPath)
40 | {
41 | foreach ($analyzerFilePath in Get-ChildItem $languageAnalyzersPath -Filter *.dll)
42 | {
43 | if($project.Object.AnalyzerReferences)
44 | {
45 | try
46 | {
47 | $project.Object.AnalyzerReferences.Remove($analyzerFilePath.FullName)
48 | }
49 | catch
50 | {
51 |
52 | }
53 | }
54 | }
55 | }
56 | }
--------------------------------------------------------------------------------
/.github/workflows/publish.yml:
--------------------------------------------------------------------------------
1 | name: publish to nuget
2 | on:
3 | push:
4 | branches:
5 | - master # Default release branch
6 | jobs:
7 | publish:
8 | name: build, pack & publish
9 | runs-on: ubuntu-latest
10 | steps:
11 | - uses: actions/checkout@v2
12 |
13 | - name: Setup .NET Core
14 | uses: actions/setup-dotnet@v1
15 | with:
16 | dotnet-version: 5.0.100
17 |
18 | # Publish
19 | - name: publish on version change
20 | id: publish_nuget
21 | uses: brandedoutcast/publish-nuget@v2.5.5
22 | with:
23 | # Filepath of the project to be packaged, relative to root of repository
24 | PROJECT_FILE_PATH: Cloneable/Cloneable.csproj
25 |
26 | # NuGet package id, used for version detection & defaults to project name
27 | PACKAGE_NAME: Cloneable
28 |
29 | # Filepath with version info, relative to root of repository & defaults to PROJECT_FILE_PATH
30 | VERSION_FILE_PATH: Directory.Build.props
31 |
32 | # Regex pattern to extract version info in a capturing group
33 | # VERSION_REGEX: ^\s*(.*)<\/Version>\s*$
34 |
35 | # Useful with external providers like Nerdbank.GitVersioning, ignores VERSION_FILE_PATH & VERSION_REGEX
36 | # VERSION_STATIC: 1.0.0
37 |
38 | # Flag to toggle git tagging, enabled by default
39 | # TAG_COMMIT: true
40 |
41 | # Format of the git tag, [*] gets replaced with actual version
42 | # TAG_FORMAT: release/*
43 |
44 | # API key to authenticate with NuGet server
45 | NUGET_KEY: ${{secrets.NUGET_API_KEY}}
46 |
47 | # NuGet server uri hosting the packages, defaults to https://api.nuget.org
48 | # NUGET_SOURCE: https://api.nuget.org
49 |
50 | # Flag to toggle pushing symbols along with nuget package to the server, disabled by default
51 | # INCLUDE_SYMBOLS: false
52 |
--------------------------------------------------------------------------------
/Cloneable.Sample/Program.cs:
--------------------------------------------------------------------------------
1 | using System;
2 |
3 | namespace Cloneable.Sample
4 | {
5 | internal static class Program
6 | {
7 | [STAThread]
8 | static void Main(string[] args)
9 | {
10 | DoSimpleClone();
11 | DoSimpleExplicitClone();
12 | DoDeepClone();
13 | DoSafeDeepClone();
14 | }
15 |
16 | static void DoSimpleClone()
17 | {
18 | // Uses the Clone method on a class with no circular references
19 | var obj = new SimpleClone()
20 | {
21 | A = "salam",
22 | B = 100
23 | };
24 | var clone = obj.Clone();
25 | Console.WriteLine(clone);
26 | Console.WriteLine("Clone equals original: " + (clone == obj));
27 | Console.WriteLine();
28 | }
29 |
30 | static void DoSimpleExplicitClone()
31 | {
32 | // Uses the Clone method on a class with no circular references
33 | var obj = new SimpleCloneExplicit()
34 | {
35 | A = "salam",
36 | B = 100
37 | };
38 | var clone = obj.Clone();
39 | Console.WriteLine(clone);
40 | Console.WriteLine("Clone equals original: " + (clone == obj));
41 | Console.WriteLine();
42 | }
43 |
44 | static void DoDeepClone()
45 | {
46 | // Uses the Clone method on a class with no circular references
47 | var obj = new SimpleClone()
48 | {
49 | A = "salam",
50 | B = 100
51 | };
52 | var deep = new DeepClone()
53 | {
54 | A = "first",
55 | Simple = obj
56 | };
57 | var clone = deep.Clone();
58 | Console.WriteLine(clone);
59 | Console.WriteLine("Clone equals original: " + (clone == deep));
60 | Console.WriteLine();
61 | }
62 |
63 | static void DoSafeDeepClone()
64 | {
65 | // Uses the Clone method on a class with no circular references
66 | var child = new SafeDeepCloneChild()
67 | {
68 | A = "child"
69 | };
70 | var parent = new SafeDeepClone()
71 | {
72 | A = "parent",
73 | Child = child
74 | };
75 | child.Parent = parent;
76 | var clone = parent.CloneSafe();
77 | Console.WriteLine(clone);
78 | Console.WriteLine("Clone equals original: " + (clone == parent));
79 | Console.WriteLine("Is parents child copied: " + (clone.Child != parent.Child));
80 | Console.WriteLine();
81 | }
82 | }
83 | }
84 |
--------------------------------------------------------------------------------
/Cloneable.sln:
--------------------------------------------------------------------------------
1 |
2 | Microsoft Visual Studio Solution File, Format Version 12.00
3 | # Visual Studio 15
4 | VisualStudioVersion = 15.0.26124.0
5 | MinimumVisualStudioVersion = 15.0.26124.0
6 | Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Cloneable", "Cloneable\Cloneable.csproj", "{8239F685-5966-47A9-BDBB-1762F6268A10}"
7 | EndProject
8 | Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Cloneable.Sample", "Cloneable.Sample\Cloneable.Sample.csproj", "{D4425270-3428-4B0D-A084-2747C9E571F8}"
9 | EndProject
10 | Global
11 | GlobalSection(SolutionConfigurationPlatforms) = preSolution
12 | Debug|Any CPU = Debug|Any CPU
13 | Debug|x64 = Debug|x64
14 | Debug|x86 = Debug|x86
15 | Release|Any CPU = Release|Any CPU
16 | Release|x64 = Release|x64
17 | Release|x86 = Release|x86
18 | EndGlobalSection
19 | GlobalSection(SolutionProperties) = preSolution
20 | HideSolutionNode = FALSE
21 | EndGlobalSection
22 | GlobalSection(ProjectConfigurationPlatforms) = postSolution
23 | {8239F685-5966-47A9-BDBB-1762F6268A10}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
24 | {8239F685-5966-47A9-BDBB-1762F6268A10}.Debug|Any CPU.Build.0 = Debug|Any CPU
25 | {8239F685-5966-47A9-BDBB-1762F6268A10}.Debug|x64.ActiveCfg = Debug|Any CPU
26 | {8239F685-5966-47A9-BDBB-1762F6268A10}.Debug|x64.Build.0 = Debug|Any CPU
27 | {8239F685-5966-47A9-BDBB-1762F6268A10}.Debug|x86.ActiveCfg = Debug|Any CPU
28 | {8239F685-5966-47A9-BDBB-1762F6268A10}.Debug|x86.Build.0 = Debug|Any CPU
29 | {8239F685-5966-47A9-BDBB-1762F6268A10}.Release|Any CPU.ActiveCfg = Release|Any CPU
30 | {8239F685-5966-47A9-BDBB-1762F6268A10}.Release|Any CPU.Build.0 = Release|Any CPU
31 | {8239F685-5966-47A9-BDBB-1762F6268A10}.Release|x64.ActiveCfg = Release|Any CPU
32 | {8239F685-5966-47A9-BDBB-1762F6268A10}.Release|x64.Build.0 = Release|Any CPU
33 | {8239F685-5966-47A9-BDBB-1762F6268A10}.Release|x86.ActiveCfg = Release|Any CPU
34 | {8239F685-5966-47A9-BDBB-1762F6268A10}.Release|x86.Build.0 = Release|Any CPU
35 | {D4425270-3428-4B0D-A084-2747C9E571F8}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
36 | {D4425270-3428-4B0D-A084-2747C9E571F8}.Debug|Any CPU.Build.0 = Debug|Any CPU
37 | {D4425270-3428-4B0D-A084-2747C9E571F8}.Debug|x64.ActiveCfg = Debug|Any CPU
38 | {D4425270-3428-4B0D-A084-2747C9E571F8}.Debug|x64.Build.0 = Debug|Any CPU
39 | {D4425270-3428-4B0D-A084-2747C9E571F8}.Debug|x86.ActiveCfg = Debug|Any CPU
40 | {D4425270-3428-4B0D-A084-2747C9E571F8}.Debug|x86.Build.0 = Debug|Any CPU
41 | {D4425270-3428-4B0D-A084-2747C9E571F8}.Release|Any CPU.ActiveCfg = Release|Any CPU
42 | {D4425270-3428-4B0D-A084-2747C9E571F8}.Release|Any CPU.Build.0 = Release|Any CPU
43 | {D4425270-3428-4B0D-A084-2747C9E571F8}.Release|x64.ActiveCfg = Release|Any CPU
44 | {D4425270-3428-4B0D-A084-2747C9E571F8}.Release|x64.Build.0 = Release|Any CPU
45 | {D4425270-3428-4B0D-A084-2747C9E571F8}.Release|x86.ActiveCfg = Release|Any CPU
46 | {D4425270-3428-4B0D-A084-2747C9E571F8}.Release|x86.Build.0 = Release|Any CPU
47 | EndGlobalSection
48 | EndGlobal
49 |
--------------------------------------------------------------------------------
/.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 | [Oo]utput/
21 | x64/
22 | x86/
23 | bld/
24 | [Bb]in/
25 | [Oo]bj/
26 | [Ll]og/
27 |
28 | # Visual Studio 2015/2017 cache/options directory
29 | .vs/
30 | # Uncomment if you have tasks that create the project's static files in wwwroot
31 | #wwwroot/
32 |
33 | # Visual Studio 2017 auto generated files
34 | Generated\ Files/
35 |
36 | # MSTest test Results
37 | [Tt]est[Rr]esult*/
38 | [Bb]uild[Ll]og.*
39 |
40 | # NUNIT
41 | *.VisualState.xml
42 | TestResult.xml
43 |
44 | # Build Results of an ATL Project
45 | [Dd]ebugPS/
46 | [Rr]eleasePS/
47 | dlldata.c
48 |
49 | # Benchmark Results
50 | BenchmarkDotNet.Artifacts/
51 |
52 | # .NET Core
53 | project.lock.json
54 | project.fragment.lock.json
55 | artifacts/
56 | **/Properties/launchSettings.json
57 |
58 | # StyleCop
59 | StyleCopReport.xml
60 |
61 | # Files built by Visual Studio
62 | *_i.c
63 | *_p.c
64 | *_i.h
65 | *.ilk
66 | *.meta
67 | *.obj
68 | *.iobj
69 | *.pch
70 | *.pdb
71 | *.ipdb
72 | *.pgc
73 | *.pgd
74 | *.rsp
75 | *.sbr
76 | *.tlb
77 | *.tli
78 | *.tlh
79 | *.tmp
80 | *.tmp_proj
81 | *.log
82 | *.vspscc
83 | *.vssscc
84 | .builds
85 | *.pidb
86 | *.svclog
87 | *.scc
88 |
89 | # Chutzpah Test files
90 | _Chutzpah*
91 |
92 | # Visual C++ cache files
93 | ipch/
94 | *.aps
95 | *.ncb
96 | *.opendb
97 | *.opensdf
98 | *.sdf
99 | *.cachefile
100 | *.VC.db
101 | *.VC.VC.opendb
102 |
103 | # Visual Studio profiler
104 | *.psess
105 | *.vsp
106 | *.vspx
107 | *.sap
108 |
109 | # Visual Studio Trace Files
110 | *.e2e
111 |
112 | # TFS 2012 Local Workspace
113 | $tf/
114 |
115 | # Guidance Automation Toolkit
116 | *.gpState
117 |
118 | # ReSharper is a .NET coding add-in
119 | _ReSharper*/
120 | *.[Rr]e[Ss]harper
121 | *.DotSettings.user
122 |
123 | # JustCode is a .NET coding add-in
124 | .JustCode
125 |
126 | # TeamCity is a build add-in
127 | _TeamCity*
128 |
129 | # DotCover is a Code Coverage Tool
130 | *.dotCover
131 |
132 | # AxoCover is a Code Coverage Tool
133 | .axoCover/*
134 | !.axoCover/settings.json
135 |
136 | # Visual Studio code coverage results
137 | *.coverage
138 | *.coveragexml
139 |
140 | # NCrunch
141 | _NCrunch_*
142 | .*crunch*.local.xml
143 | nCrunchTemp_*
144 |
145 | # MightyMoose
146 | *.mm.*
147 | AutoTest.Net/
148 |
149 | # Web workbench (sass)
150 | .sass-cache/
151 |
152 | # Installshield output folder
153 | [Ee]xpress/
154 |
155 | # DocProject is a documentation generator add-in
156 | DocProject/buildhelp/
157 | DocProject/Help/*.HxT
158 | DocProject/Help/*.HxC
159 | DocProject/Help/*.hhc
160 | DocProject/Help/*.hhk
161 | DocProject/Help/*.hhp
162 | DocProject/Help/Html2
163 | DocProject/Help/html
164 |
165 | # Click-Once directory
166 | publish/
167 |
168 | # Publish Web Output
169 | *.[Pp]ublish.xml
170 | *.azurePubxml
171 | # Note: Comment the next line if you want to checkin your web deploy settings,
172 | # but database connection strings (with potential passwords) will be unencrypted
173 | *.pubxml
174 | *.publishproj
175 |
176 | # Microsoft Azure Web App publish settings. Comment the next line if you want to
177 | # checkin your Azure Web App publish settings, but sensitive information contained
178 | # in these scripts will be unencrypted
179 | PublishScripts/
180 |
181 | # NuGet Packages
182 | *.nupkg
183 | # The packages folder can be ignored because of Package Restore
184 | **/[Pp]ackages/*
185 | # except build/, which is used as an MSBuild target.
186 | !**/[Pp]ackages/build/
187 | # Uncomment if necessary however generally it will be regenerated when needed
188 | #!**/[Pp]ackages/repositories.config
189 | # NuGet v3's project.json files produces more ignorable files
190 | *.nuget.props
191 | *.nuget.targets
192 |
193 | # Microsoft Azure Build Output
194 | csx/
195 | *.build.csdef
196 |
197 | # Microsoft Azure Emulator
198 | ecf/
199 | rcf/
200 |
201 | # Windows Store app package directories and files
202 | AppPackages/
203 | BundleArtifacts/
204 | Package.StoreAssociation.xml
205 | _pkginfo.txt
206 | *.appx
207 |
208 | # Visual Studio cache files
209 | # files ending in .cache can be ignored
210 | *.[Cc]ache
211 | # but keep track of directories ending in .cache
212 | !*.[Cc]ache/
213 |
214 | # Others
215 | ClientBin/
216 | ~$*
217 | *~
218 | *.dbmdl
219 | *.dbproj.schemaview
220 | *.jfm
221 | *.pfx
222 | *.publishsettings
223 | orleans.codegen.cs
224 |
225 | # Including strong name files can present a security risk
226 | # (https://github.com/github/gitignore/pull/2483#issue-259490424)
227 | #*.snk
228 |
229 | # Since there are multiple workflows, uncomment next line to ignore bower_components
230 | # (https://github.com/github/gitignore/pull/1529#issuecomment-104372622)
231 | #bower_components/
232 |
233 | # RIA/Silverlight projects
234 | Generated_Code/
235 |
236 | # Backup & report files from converting an old project file
237 | # to a newer Visual Studio version. Backup files are not needed,
238 | # because we have git ;-)
239 | _UpgradeReport_Files/
240 | Backup*/
241 | UpgradeLog*.XML
242 | UpgradeLog*.htm
243 | ServiceFabricBackup/
244 | *.rptproj.bak
245 |
246 | # SQL Server files
247 | *.mdf
248 | *.ldf
249 | *.ndf
250 |
251 | # Business Intelligence projects
252 | *.rdl.data
253 | *.bim.layout
254 | *.bim_*.settings
255 | *.rptproj.rsuser
256 |
257 | # Microsoft Fakes
258 | FakesAssemblies/
259 |
260 | # GhostDoc plugin setting file
261 | *.GhostDoc.xml
262 |
263 | # Node.js Tools for Visual Studio
264 | .ntvs_analysis.dat
265 | node_modules/
266 |
267 | # Visual Studio 6 build log
268 | *.plg
269 |
270 | # Visual Studio 6 workspace options file
271 | *.opt
272 |
273 | # Visual Studio 6 auto-generated workspace file (contains which files were open etc.)
274 | *.vbw
275 |
276 | # Visual Studio LightSwitch build output
277 | **/*.HTMLClient/GeneratedArtifacts
278 | **/*.DesktopClient/GeneratedArtifacts
279 | **/*.DesktopClient/ModelManifest.xml
280 | **/*.Server/GeneratedArtifacts
281 | **/*.Server/ModelManifest.xml
282 | _Pvt_Extensions
283 |
284 | # Paket dependency manager
285 | .paket/paket.exe
286 | paket-files/
287 |
288 | # FAKE - F# Make
289 | .fake/
290 |
291 | # JetBrains Rider
292 | .idea/
293 | *.sln.iml
294 |
295 | # CodeRush
296 | .cr/
297 |
298 | # Python Tools for Visual Studio (PTVS)
299 | __pycache__/
300 | *.pyc
301 |
302 | # Cake - Uncomment if you are using it
303 | # tools/**
304 | # !tools/packages.config
305 |
306 | # Tabs Studio
307 | *.tss
308 |
309 | # Telerik's JustMock configuration file
310 | *.jmconfig
311 |
312 | # BizTalk build output
313 | *.btp.cs
314 | *.btm.cs
315 | *.odx.cs
316 | *.xsd.cs
317 |
318 | # OpenCover UI analysis results
319 | OpenCover/
320 |
321 | # Azure Stream Analytics local run output
322 | ASALocalRun/
323 |
324 | # MSBuild Binary and Structured Log
325 | *.binlog
326 |
327 | # NVidia Nsight GPU debugger configuration file
328 | *.nvuser
329 |
330 | # MFractors (Xamarin productivity tool) working folder
331 | .mfractor/
332 |
--------------------------------------------------------------------------------
/Cloneable/CloneableGenerator.cs:
--------------------------------------------------------------------------------
1 | using Microsoft.CodeAnalysis;
2 | using Microsoft.CodeAnalysis.CSharp;
3 | using Microsoft.CodeAnalysis.CSharp.Syntax;
4 | using Microsoft.CodeAnalysis.Text;
5 | using System;
6 | using System.Collections.Generic;
7 | using System.Linq;
8 | using System.Text;
9 |
10 | namespace Cloneable
11 | {
12 | [Generator]
13 | public class CloneableGenerator : ISourceGenerator
14 | {
15 | private const string PreventDeepCopyKeyString = "PreventDeepCopy";
16 | private const string ExplicitDeclarationKeyString = "ExplicitDeclaration";
17 |
18 | private const string CloneableNamespace = "Cloneable";
19 | private const string CloneableAttributeString = "CloneableAttribute";
20 | private const string CloneAttributeString = "CloneAttribute";
21 | private const string IgnoreCloneAttributeString = "IgnoreCloneAttribute";
22 |
23 | private const string cloneableAttributeText = @"using System;
24 |
25 | namespace " + CloneableNamespace + @"
26 | {
27 | [AttributeUsage(AttributeTargets.Class | AttributeTargets.Struct, Inherited = true, AllowMultiple = false)]
28 | public sealed class " + CloneableAttributeString + @" : Attribute
29 | {
30 | public " + CloneableAttributeString + @"()
31 | {
32 | }
33 |
34 | public bool " + ExplicitDeclarationKeyString + @" { get; set; }
35 | }
36 | }
37 | ";
38 |
39 | private const string clonePropertyAttributeText = @"using System;
40 |
41 | namespace " + CloneableNamespace + @"
42 | {
43 | [AttributeUsage(AttributeTargets.Property, Inherited = true, AllowMultiple = false)]
44 | public sealed class " + CloneAttributeString + @" : Attribute
45 | {
46 | public " + CloneAttributeString + @"()
47 | {
48 | }
49 |
50 | public bool " + PreventDeepCopyKeyString + @" { get; set; }
51 | }
52 | }
53 | ";
54 |
55 | private const string ignoreClonePropertyAttributeText = @"using System;
56 |
57 | namespace " + CloneableNamespace + @"
58 | {
59 | [AttributeUsage(AttributeTargets.Property, Inherited = true, AllowMultiple = false)]
60 | public sealed class " + IgnoreCloneAttributeString + @" : Attribute
61 | {
62 | public " + IgnoreCloneAttributeString + @"()
63 | {
64 | }
65 | }
66 | }
67 | ";
68 |
69 | private INamedTypeSymbol? cloneableAttribute;
70 | private INamedTypeSymbol? ignoreCloneAttribute;
71 | private INamedTypeSymbol? cloneAttribute;
72 |
73 | public void Initialize(GeneratorInitializationContext context)
74 | {
75 | context.RegisterForSyntaxNotifications(() => new SyntaxReceiver());
76 | }
77 |
78 | public void Execute(GeneratorExecutionContext context)
79 | {
80 | InjectCloneableAttributes(context);
81 | GenerateCloneMethods(context);
82 | }
83 |
84 | private void GenerateCloneMethods(GeneratorExecutionContext context)
85 | {
86 | if (context.SyntaxReceiver is not SyntaxReceiver receiver)
87 | return;
88 |
89 | Compilation compilation = GetCompilation(context);
90 |
91 | InitAttributes(compilation);
92 |
93 | var classSymbols = GetClassSymbols(compilation, receiver);
94 | foreach (var classSymbol in classSymbols)
95 | {
96 | if (!classSymbol.TryGetAttribute(cloneableAttribute!, out var attributes))
97 | continue;
98 |
99 | var attribute = attributes.Single();
100 | var isExplicit = (bool?)attribute.NamedArguments.FirstOrDefault(e => e.Key.Equals(ExplicitDeclarationKeyString)).Value.Value ?? false;
101 | context.AddSource($"{classSymbol.Name}_cloneable.cs", SourceText.From(CreateCloneableCode(classSymbol, isExplicit), Encoding.UTF8));
102 | }
103 | }
104 |
105 | private void InitAttributes(Compilation compilation)
106 | {
107 | cloneableAttribute = compilation.GetTypeByMetadataName($"{CloneableNamespace}.{CloneableAttributeString}")!;
108 | cloneAttribute = compilation.GetTypeByMetadataName($"{CloneableNamespace}.{CloneAttributeString}")!;
109 | ignoreCloneAttribute = compilation.GetTypeByMetadataName($"{CloneableNamespace}.{IgnoreCloneAttributeString}")!;
110 | }
111 |
112 | private static Compilation GetCompilation(GeneratorExecutionContext context)
113 | {
114 | var options = context.Compilation.SyntaxTrees.First().Options as CSharpParseOptions;
115 |
116 | var compilation = context.Compilation.AddSyntaxTrees(CSharpSyntaxTree.ParseText(SourceText.From(cloneableAttributeText, Encoding.UTF8), options)).
117 | AddSyntaxTrees(CSharpSyntaxTree.ParseText(SourceText.From(clonePropertyAttributeText, Encoding.UTF8), options)).
118 | AddSyntaxTrees(CSharpSyntaxTree.ParseText(SourceText.From(ignoreClonePropertyAttributeText, Encoding.UTF8), options));
119 | return compilation;
120 | }
121 |
122 | private string CreateCloneableCode(INamedTypeSymbol classSymbol, bool isExplicit)
123 | {
124 | string namespaceName = classSymbol.ContainingNamespace.ToDisplayString();
125 | var fieldAssignmentsCode = GenerateFieldAssignmentsCode(classSymbol, isExplicit);
126 | var fieldAssignmentsCodeSafe = fieldAssignmentsCode.Select(x =>
127 | {
128 | if (x.isCloneable)
129 | return x.line + "Safe(referenceChain)";
130 | return x.line;
131 | });
132 | var fieldAssignmentsCodeFast = fieldAssignmentsCode.Select(x =>
133 | {
134 | if (x.isCloneable)
135 | return x.line + "()";
136 | return x.line;
137 | });
138 |
139 | return $@"using System.Collections.Generic;
140 |
141 | namespace {namespaceName}
142 | {{
143 | {GetAccessModifier(classSymbol)} partial class {classSymbol.Name}
144 | {{
145 | ///
146 | /// Creates a copy of {classSymbol.Name} with NO circular reference checking. This method should be used if performance matters.
147 | ///
148 | /// Will occur on any object that has circular references in the hierarchy.
149 | ///
150 | public {classSymbol.Name} Clone()
151 | {{
152 | return new {classSymbol.Name}
153 | {{
154 | {string.Join($",{Environment.NewLine}", fieldAssignmentsCodeFast)}
155 | }};
156 | }}
157 |
158 | ///
159 | /// Creates a copy of {classSymbol.Name} with circular reference checking. If a circular reference was detected, only a reference of the leaf object is passed instead of cloning it.
160 | ///
161 | /// Should only be provided if specific objects should not be cloned but passed by reference instead.
162 | public {classSymbol.Name} CloneSafe(Stack