使用Roslyn for C#,如何获得组成返回类型的所有属性的列表?

时间:2014-01-23 18:54:01

标签: c# roslyn

假设我从一组方法中查询了一个方法:

var myMethod = someListofMethods.FirstOrDefault(m => m.Identifier.ValueText == myMethodName);

现在我想采用方法的返回类型。 。

var returnType = myMethod.ReturnType;

。 。 。并确定(如果它不是原始的)该类型中包含哪些属性。

因此,例如,假设返回类型为FooObject,其定义为:

public class FooObject{
     public string Fizz {get; set; }
     public string Buzz {get; set; }
}

如何正确查询FooObject以查找其属性列表?

这是我已经尝试过的:

returnType.DescendantNodes().OfType<PropertyDeclarationSyntax>();

但是这没用。提前谢谢。

2 个答案:

答案 0 :(得分:10)

您正在查看abstract syntax tree (AST)级别的代码。因此行:

returnType.DescendantNodes().OfType<PropertyDeclarationSyntax>();

什么都不返回。此上下文中的returnType是AST的IdentifierNameSyntax节点,只包含文本FooObject。如果要分析返回类型,您应该:

  • returnType角度解释语法树以查找返回类型的完整命名空间
  • 搜索代码以查找此类型声明
  • 分析类型声明语法树以查找其所有属性

但是,实际上是编译器所做的,所以你可以将Roslyn的使用升级到编译级别,例如:

var workspace = Workspace.LoadSolution(solutionName);
var solution = workspace.CurrentSolution;

var createCommandList = new List<ISymbol>();
var @class = solution.Projects.Select(s => s.GetCompilation()
                                            .GetTypeByMetadataName(className))
                              .FirstOrDefault();
var method = @class.GetMembers(methodName)
                    .AsList()
                    .Where(s => s.Kind == CommonSymbolKind.Method)
                    .Cast<MethodSymbol>()
                    .FirstOrDefault();
var returnType = method.ReturnType as TypeSymbol;
var returnTypeProperties = returnType.GetMembers()
                                     .AsList()
                                     .Where(s => s.Kind == SymbolKind.Property)
                                     .Select(s => s.Name);

答案 1 :(得分:0)

创建下面的类CsharpClass.cs

public class CsharpClass
{
    public string Name { get; set; }

    public string Namespace { get; set; }

    public List<CsharpProperty> Properties { get; set; }

    public string PrimaryKeyType { get; set; }

    public class CsharpProperty
    {
        public string Name { get; set; }
        public string Type { get; set; }

        public CsharpProperty(string name, string type)
        {
            Name = name;
            Type = type;
        }
    }

    public CsharpClass()
    {
        Properties = new List<CsharpProperty>();
    }
}

创建以下帮助程序类CsharpClassParser.cs

public static class CsharpClassParser
{
    public static CsharpClass Parse(string content)
    {
        var cls = new CsharpClass();
        var tree = CSharpSyntaxTree.ParseText(content);
        var members = tree.GetRoot().DescendantNodes().OfType<MemberDeclarationSyntax>();

        foreach (var member in members)
        {
            if (member is PropertyDeclarationSyntax property)
            {
                cls.Properties.Add(new CsharpClass.CsharpProperty(
                     property.Identifier.ValueText,
                     property.Type.ToString())
                 );
            }

            if (member is NamespaceDeclarationSyntax namespaceDeclaration)
            {
                cls.Namespace = namespaceDeclaration.Name.ToString();
            }

            if (member is ClassDeclarationSyntax classDeclaration)
            {
                cls.Name = classDeclaration.Identifier.ValueText;

                cls.PrimaryKeyType = FindPrimaryKeyType(classDeclaration);
            }

            //if (member is MethodDeclarationSyntax method)
            //{
            //    Console.WriteLine("Method: " + method.Identifier.ValueText);
            //}
        }


        return cls;
    }

    private static string FindPrimaryKeyType(ClassDeclarationSyntax classDeclaration)
    {
        if (classDeclaration == null)
        {
            return null;
        }

        if (classDeclaration.BaseList == null)
        {
            return null;
        }

        foreach (var baseClass in classDeclaration.BaseList.Types)
        {
            var match = Regex.Match(baseClass.Type.ToString(), @"<(.*?)>");
            if (match.Success)
            {
                var primaryKey = match.Groups[1].Value;

                if (AppConsts.PrimaryKeyTypes.Any(x => x.Value == primaryKey))
                {
                    return primaryKey;
                }
            }
        }

        return null;
    }
}

如何使用?

以字符串形式读取类的内容,并将其传递给CsharpClassParser.Parse()

const string content = @"
namespace Acme.Airlines.AirCraft
{
    public class AirCraft
    {
        public virtual string Name { get; set; }

        public virtual int Code { get; set; }

        public AirCraft()
        {

        }
    }
}";

var csharpClass = CsharpClassParser.Parse(content);

Console.WriteLine(csharpClass.Name);
Console.WriteLine(csharpClass.Namespace);
Console.WriteLine(csharpClass.Properties.Count);