Skip to content

Instantly share code, notes, and snippets.

@explorer14
Last active July 17, 2021 22:42
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/f68a03cbb9cfee64ae4b887926e1c528 to your computer and use it in GitHub Desktop.
Save explorer14/f68a03cbb9cfee64ae4b887926e1c528 to your computer and use it in GitHub Desktop.
internal static string BuildDtoProperty(
this PropertyDeclarationSyntax pds,
Compilation compilation)
{
var blah = compilation
.GetSemanticModel(pds.SyntaxTree)
.GetDeclaredSymbol(pds);
var propSym = (blah as IPropertySymbol);
if (propSym.IsOfTypeClass() || propSym.IsOfTypeStruct())
{
if (propSym.Type.NullableAnnotation == NullableAnnotation.Annotated)
return $"public {propSym.Type.Name}Dto? {property.Name} {{get; set;}}";
return $"public {propSym.Type.Name()}Dto {property.Name} {{get; set;}}";
}
else
return $"public {propSym.Type.Name()} {property.Name} {{get; set;}}";
}
internal static bool IsOfTypeClass(this IPropertySymbol propSym) =>
propSym.Type.IsReferenceType &&
propSym.Type.TypeKind == TypeKind.Class &&
propSym.Type.ToDisplayString().StartsWith(
propSym.ContainingNamespace.ToDisplayString());
internal static bool IsOfTypeStruct(this IPropertySymbol propSym) =>
propSym.Type.IsValueType &&
propSym.Type.TypeKind == TypeKind.Struct &&
propSym.Type.ToDisplayString().StartsWith(
propSym.ContainingNamespace.ToDisplayString());
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment