Good day please am using t4 template to autogenerate viewmodel for all my models. I Started by creating a BaseViewModel from which all viewModels inherits, using roslyn to analyse and all classes that inherit from BaseViewModel and then reads all properties of the model which I'll then put into the respective viewmodels. But inherited properties of the Model was never present.
Here is what I've tried
public bool GetAllProperties(ClassDeclarationSyntax classSyntax)
{
if(classSyntax == null)
{
return false;
}
foreach(PropertyDeclarationSyntax prp in classSyntax.Members.Where(m => m.Kind() == SyntaxKind.PropertyDeclaration))
{
if(!declaration.DescendantNodes().OfType<PropertyDeclarationSyntax>().Any(p => p.Identifier == prp.Identifier))
{
sb.AppendLine(" public " + prp.Type + " " + prp.Identifier);
sb.AppendLine(" {");
sb.AppendLine(" get => Model." + prp.Identifier + ";");
sb.AppendLine(" set");
sb.AppendLine(" {");
//sb.AppendLine(" Model." + prp.Identifier + " = value;");
sb.AppendLine(" SetProperty(ref Model." + prp.Identifier + ", value, () => this." + prp.Identifier + ");");
//sb.AppendLine(" RaisePropertyChanged(() => " + prp.Identifier + ");");
sb.AppendLine(" }");
sb.AppendLine(" }\n");
}
}
return true;
}
I even try to get all base classes of the Model recursively but was unable to load properties or convert the BaseTypeSyntax by the ClassDeclarationSyntax.BaseList.Types to a ClassDeclarationSyntax.
public bool GetAllProperties(ClassDeclarationSyntax classSyntax)
{
if(classSyntax == null)
{
return false;
}
if(classSyntax.BaseList.Types != null && classSyntax.BaseList.Types.Any())
{
foreach(var bt in classSyntax.BaseList.Types)
{
GetAllProperties((BaseTypeDeclarationSyntax)bt);
}
}
foreach(PropertyDeclarationSyntax prp in classSyntax.Members.Where(m => m.Kind() == SyntaxKind.PropertyDeclaration))
{
if(!declaration.DescendantNodes().OfType<PropertyDeclarationSyntax>().Any(p => p.Identifier == prp.Identifier))
{
sb.AppendLine(" public " + prp.Type + " " + prp.Identifier);
sb.AppendLine(" {");
sb.AppendLine(" get => Model." + prp.Identifier + ";");
sb.AppendLine(" set");
sb.AppendLine(" {");
//sb.AppendLine(" Model." + prp.Identifier + " = value;");
sb.AppendLine(" SetProperty(ref Model." + prp.Identifier + ", value, () => this." + prp.Identifier + ");");
//sb.AppendLine(" RaisePropertyChanged(() => " + prp.Identifier + ");");
sb.AppendLine(" }");
sb.AppendLine(" }\n");
}
}
return true;
}
Please help me, am new to both t4 template and roslyn, am just 7 days into them. Thanks in advance.
Edit
After some hours of googling I was able to come up with something like this
IEnumerable<PropertyDeclarationSyntax> GetAllProperties(ClassDeclarationSyntax classSyntax)
{
var result = new List<PropertyDeclarationSyntax>();
if(classSyntax == null)
{
return result;
}
if(classSyntax.BaseList.Types != null && classSyntax.BaseList.Types.Any())
{
foreach(var bt in classSyntax.BaseList.Types)
{
result.AddRange(GetAllProperties2(bt));
}
}
foreach(PropertyDeclarationSyntax prp in classSyntax.Members.Where(m => m.Kind() == SyntaxKind.PropertyDeclaration))
{
result.Add(prp);
}
return result;
}
public IEnumerable<PropertyDeclarationSyntax> GetAllProperties2(BaseTypeSyntax typeSyntax)
{
var result = new List<PropertyDeclarationSyntax>();
if(typeSyntax == null)
{
return result;
}
if(GetBaseList(typeSyntax).Types != null)
{
foreach(var bt in GetBaseList(typeSyntax).Types)
{
result.AddRange(GetAllProperties2(bt));
}
}
foreach(PropertyDeclarationSyntax prp in GetMemberList(typeSyntax).Where(m => m.Kind() == SymbolKind.Property))
{
result.Add(prp);
}
return result;
}
public BaseListSyntax GetBaseList(BaseTypeSyntax type)
{
return SyntaxFactory.BaseList(SyntaxFactory.SingletonSeparatedList<BaseTypeSyntax>(SyntaxFactory.SimpleBaseType(type.Type)));
}
public PropertyDeclarationSyntax GetMemberList(BaseTypeSyntax type)
{
return SyntaxFactory.PropertyDeclaration(type.Type, SyntaxFactory.Token(type.Kind()));
}
But the method GetMemberList only return a single PropertyDeclarationSyntax from a TypeSyntax whereas I want a List of all the PropertyDeclarationSyntax of a TypeSyntax