获取对象的类型,该方法被调用

时间:2017-02-08 11:37:56

标签: roslyn roslyn-code-analysis

我有这个代码块,我想知道对象的类型" Any"方法被调用。例如,在这种情况下,我的预期结果是DbSet

ConnectionStringSQL = "Provider=SQLOLEDB;Data Source=PCName\SQLEXPRESS;Network Library=DBMSSOCN;Initial Catalog=MyDBName;Username=MyUsername;Password=MyPassword"

或者在这个例子中:

using (AppDbContext context = new AppDbContext() /*Inherited from DbContext of EntityFramework*/)

{
     context.Products.Any(); // context.Products is type of DbSet<Product>
}

我的预期结果是int []

2 个答案:

答案 0 :(得分:1)

找到您感兴趣的SyntaxNode,获取Compilation,然后致电SemanticModel.GetSymbolInfo()

答案 1 :(得分:0)

使用SemanticModel.GetTypeInfo

例如,如果您有SyntaxNodeAnalysisContext contextExpressionSyntax expression,则可以

var type = context.SemanticModel.GetTypeInfo(expression, context.CancellationToken).Type;