Eugene Shelukhin
Eugene Shelukhin

Reputation: 83

Why does ITypeSymbol.ToString() return a wrong value for a multidimensional array in Roslyn?

My aim is to get TypeSyntax by ITypeSymbol with Roslyn.
I do it this way:

SF.ParseTypeName(myTypeSymbol.ToString())

This approach works fine until we get myTypeSymbol of multidimensional array in input.
Example:
Say myExpression is of type int[,] in this case

SemanticModel.GetTypeInfo(myExpression).Type.ToString();

returns int[*,*] instead of int[,].
So SF.ParseTypeName("int[*,*]") returns wrong TypeSyntax.
In the result TypeSyntax rank.sizes are parsed as PrefixUnaryExpressionSyntax instead of OmittedArraySizeExpressionSyntax.

So the questions are why does myTypeSymbol.ToString() returns int[*,*]? and is there any workaround to get correct TypeSyntax for multidimensional array?

Upvotes: 0

Views: 236

Answers (1)

Paulo Morgado
Paulo Morgado

Reputation: 14846

If you want to control the string representation of a symbol (to be programming language specific, for example), you need to use a SymbolDisplayFormat and feed it to 'symbol.ToDisplayString()`.

Upvotes: 1

Related Questions