Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Getting the type of expression result in Roslyn

Tags:

c#

roslyn

If I have an expression producing a result, say, new ClassA(), or "somestring", or whatever, and I have a corresponding ExpressionSyntax object, how do I get the INamedTypeSymbol object corresponding to the result of the expression?

Many tutorials mention the GetTypeInfo method, but I cannot find it in the latest Roslyn I got from NuGet. Is there something that replaces it?

Update: Somehow I had the 1.0 version of Roslyn. After updating it to 1.2, I found the GetTypeInfo method.

like image 498
ulu Avatar asked Oct 22 '13 09:10

ulu


1 Answers

You need to build the semantic model, like this:

var semanticModel = document.GetSemanticModel(cancellationToken);
var typeInfo = semanticModel.GetTypeInfo(expression, cancellationToken);
like image 138
Kris Vandermotten Avatar answered Oct 19 '22 23:10

Kris Vandermotten