Ramin Pedram
Ramin Pedram

Reputation: 33

Get Type Of Object which Method Is Called On That

I've this code block and I want to know the type of object which "Any" method is called on that. For example in this scenario my expected result is DbSet

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

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

Or in this example:

new int[]{}.Any()

my expected result is int[]

Upvotes: 2

Views: 86

Answers (2)

Kris Vandermotten
Kris Vandermotten

Reputation: 10201

Use SemanticModel.GetTypeInfo.

For example, if you have a SyntaxNodeAnalysisContext context and an ExpressionSyntax expression, you would do

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

Upvotes: 0

SLaks
SLaks

Reputation: 887453

Find the SyntaxNode you're interested in, get its Compilation, then call SemanticModel.GetSymbolInfo().

Upvotes: 1

Related Questions