/TrackGenericPropreties.cs Secret
Last active
July 19, 2021 17:52
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
var propertiesWithCollectionTypes = | |
new List<(IPropertySymbol Symbol, PropertyDeclarationSyntax Syntax)>(); | |
foreach (var item in typeNode.Members) | |
{ | |
if (item is PropertyDeclarationSyntax pds) | |
{ | |
var symbol = context.Compilation | |
.GetSemanticModel(pds.SyntaxTree) | |
.GetDeclaredSymbol(pds); | |
var property = (symbol as IPropertySymbol); | |
var propertyType = property.Type as INamedTypeSymbol; | |
if (propertyType.IsGenericType) | |
{ | |
if (property.IsAtleastOneTypeArgumentCustomType()) | |
{ | |
codeBuilder.AppendLine($"\t\t\t\t\t{property.Name} = entity.{property.Name}.ToDto(),"); | |
propertiesWithCollectionTypes.Add((property, pds)); | |
} | |
else | |
codeBuilder.AppendLine($"\t\t\t\t\t{property.Name} = entity.{property.Name},"); | |
} | |
else | |
{ | |
// non generic properties | |
} | |
} | |
} | |
... | |
internal static bool IsAtleastOneTypeArgumentCustomType( | |
this IPropertySymbol property) | |
{ | |
var typeArgs = (property.Type as INamedTypeSymbol)!.TypeArguments; | |
foreach (var type in typeArgs) | |
{ | |
var isCustom = property.IsPropertyTypeCustom(type); | |
if (isCustom) | |
return true; | |
} | |
return false; | |
} | |
internal static bool IsPropertyTypeCustom(this IPropertySymbol property, | |
ITypeSymbol type) => | |
type.ToDisplayString().StartsWith( | |
property.ContainingNamespace.ToDisplayString()); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment