Skip to content

Instantly share code, notes, and snippets.

@explorer14
Last active July 19, 2021 17:52
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save explorer14/5b1fdc19b519035c83b27c999669ad26 to your computer and use it in GitHub Desktop.
Save explorer14/5b1fdc19b519035c83b27c999669ad26 to your computer and use it in GitHub Desktop.
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