| name | CopyConstructor + Deconstructor |
| nuget |
https://www.nuget.org/packages/AOPMethodsCommon/ |
| 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
[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
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
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
Leave a Reply