I m experimenting a bit with the Roslyn-CTP.
Currently I m trying to replace var
with the concrete type.
var i=1;
应当成为:
int i=1;
简单明了所推断的类型。 但是,由于这一部分出现在语法模式中,因此,我收到了一份global:System.Int32
),转换的背景取决于(using
, nested categories等)。
The Visual studio version that s part of Roslyn already has this functionality in its "Simplify type name" quickfix, but looking over the samples I couldn t find an easy way to do this conversion.
Based on Kevin Pilch-Bisson s answer I m now using:
var location = document.GetSyntaxTree().GetLocation(node);
string name = variableType.ToMinimalDisplayString((Location)location, (SemanticModel)document.GetSemanticModel());
A location which ToMinimalDisplayString
can be obtained from a CommonSyntaxTree
.
An additional complication is that ToMinimalDisplayString
requires the classes Location
and SemanticModel
, whereas document.GetSemanticModel()
and CommonSyntaxTree.GetLocation
only return an interface.
I worked around by simply casting to the classes, which seems to work for now.
该校像是C#级,接口语言独立。
I ve uploaded a working edition on github:
It doesn t work for var
in a foreach
, but I suspect that s a limitation of the current Roslyn build.