使用roslyn

时间:2019-09-16 06:33:34

标签: c# viewmodel roslyn t4 roslyn-code-analysis

美好的一天,请使用t4模板为我的所有模型自动生成viewmodel。我首先创建一个继承所有ViewModels的BaseViewModel,然后使用roslyn进行分析以及从BaseViewModel继承的所有类,然后读取该模型的所有属性,然后将它们放入相应的ViewModel中。但是从未继承过Model的继承属性。

这是我尝试过的

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;
}

我什至尝试递归获取Model的所有基类,但是无法加载属性或将ClassType声明通过ClassDeclarationSyntax.BaseList.Types转换为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;
}

请帮助我,这是t4模板和roslyn的新手,距离它们只有7天了。 预先感谢。

修改

经过几个小时的搜寻,我终于想到了类似的东西

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()));
}

但是方法 GetMemberList 仅从 TypeSyntax 返回单个 PropertyDeclarationSyntax ,而我想要所有 PropertyDeclarationSyntax 的列表 TypeSyntax

的em>

0 个答案:

没有答案