RSGC-Constructor – Deconstructor – part 5

 

 

name CopyConstructor + Deconstructor
nuget

https://www.nuget.org/packages/AOPMethodsCommon/
https://www.nuget.org/packages/AOPMethodsGenerator/

link http://msprogrammer.serviciipeweb.ro/category/roslyn/
author Andrei Ignat

This will generate code for a POCO to generate copy constructor and deconstructor
 

The code that you start with is

01
02
03
04
05
06
07
08
09
10
11
[AutoMethods(template = TemplateMethod.CustomTemplateFile, CustomTemplateFileName = "CopyConstructorDestructor.txt")]
 
partial class Person
 
{
 
   public string FirstName { get; set; }
 
   public string LastName { get; set; }
 
}

The code that you will use is

01
02
03
04
05
06
07
08
09
10
11
12
13
var pOldPerson = new Person();
 
pOldPerson.FirstName = "Andrei";
 
pOldPerson.LastName = "Ignat";
 
var newPerson = new Person(pOldPerson);
 
Console.WriteLine(newPerson.FirstName);
 
var (_, last) = newPerson;
 
Console.WriteLine(last);

 

The code that is generated is

01
02
03
04
05
06
07
08
09
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
public Person (){                                                         
 
   OnConstructor();
 
}
 
public Person(IPerson other):base(){
 
     BeforeCopyConstructor(other);
 
     CopyPropertiesFrom(other);
 
     AfterCopyConstructor(other);
 
           
 
}
 
public void CopyPropertiesFrom(IPerson other){
 
 
 
     this.FirstName = other.FirstName;           
 
     this.LastName = other.LastName;           
 
}   
 
 
 
 
 
 
 
 public void Deconstruct( out string FirstName, out string LastName)
 
 {
 
     FirstName = this.FirstName;           
 
     LastName = this.LastName;           
 
 }

Example Code: https://github.com/ignatandrei/RSCG_Examples/tree/main/CopyConstructor

All RSCG

NrBlog Post
1RSCG–part 1
2RSCG- AppVersion–part 2
3http://msprogrammer.serviciipeweb.ro/2021/02/17/rsgc-enum-part-3/
4RSGC-JSON to Class- part 4
5RSGC-Constructor – Deconstructor – part 5
6RSGC – DTO Mapper – part 6
7RSGC – Skinny Controllers- part 7
8RSGC-Builder Design Pattern – part 8
9RSGC- MetadataFromObject – part 9
10RSGC- Dynamic Mock – part 10
11RSCG- Method Decorator – part 11
12RSCG – Curry – Partial function – part 12
13RSCG- part 13 – IFormattable
14RSCG- part 14 – DP_Decorator
15RSCG- part 15 – Expression Generator
16RSCG- part 16 – Many Others
17RSCG- the book
18RSCG–Template Rendering- part 17
19CI Version
20HttpClientGenerator
21Query from database
22AutoRegister
23TinyTypes
24Static2Interface
25AppSettings
26Properties
27
Roslyn Source Code Generators