https://github.com/anupsarkar-dev/csharprecordtoclass
Convert your C# record type to class type
https://github.com/anupsarkar-dev/csharprecordtoclass
Last synced: 9 months ago
JSON representation
Convert your C# record type to class type
- Host: GitHub
- URL: https://github.com/anupsarkar-dev/csharprecordtoclass
- Owner: anupsarkar-dev
- Created: 2023-08-15T09:25:09.000Z (almost 3 years ago)
- Default Branch: master
- Last Pushed: 2023-11-16T06:21:59.000Z (over 2 years ago)
- Last Synced: 2025-03-03T01:28:04.819Z (over 1 year ago)
- Language: C#
- Size: 11.7 KB
- Stars: 0
- Watchers: 1
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
Awesome Lists containing this project
README
# CsharpRecordToClass
Convert your C# record type to class type
Often while creating POCO or DTO type , we use C# record or class type.
Many times we have to manually convert C# class to record type or vice versa which is time consuming.
Lets automate this:
for example we have a record type:
Now we want to convert it to a class or vice versa
Here is the simple C# program to do it:
```
string RecortToClass(string input) {
string recordNamePattern = @ "\s+record\s+(\w+)";
string propertyPattern = @ "\s+(\w+\??)\s+(\w+)\s*[,]?\s*$";
var result = new StringBuilder();
Match recordNameMatch = Regex.Match(input, recordNamePattern);
if (recordNameMatch.Success && recordNameMatch.Groups.Count == 2) {
string recordName = recordNameMatch.Groups[1].Value;
result.Append($"public class {recordName}");
}
// Extract properties
MatchCollection matches = Regex.Matches(input, propertyPattern, RegexOptions.Multiline);
result.Append($"\n {{");
foreach(Match match in matches) {
string type = match.Groups[1].Value; // property type
string name = match.Groups[2].Value; // property name
if (type.ToLower() != "record")
result.Append($"\n \t public {type} {name} {{ get; set; }}");
}
result.Append($"\n}}");
return result.ToString();
}
```