0

When I try to create an identifier using the Roslyn SyntaxGenerator.IdentifierName for a primitive type, it adds an escape to the Identifier.Text.

var generator = SyntaxGenerator.GetGenerator(document);

// genericTypeArg.Identifier.ValueText = "@double"
var genericTypeArg = generator.IdentifierName("double");

// generic.Identifier.ValueText = "IEnumerable<@double>"
var generic = generator.GenericName("IEnumerable", genericTypeArg);

Any ideas how to create the type identifier without it being escaped?

Edit:

Would this be a valid way to test?

var typeExp = typeSymbol.SpecialType == SpecialType.None
    ? _generator.IdentifierName(typeSymbol.ToDisplayString(symbolDisplayFormat));
    : _generator.TypeExpression(typeSymbol.SpecialType)
var generic = _generator.GenericName("IEnumerable", typeExp);

1 Answers1

1

An IdentifierName() is exactly that – the name of an identifier.

You asked for an identifier named double, and it gave you a valid syntax for that.

You need the SpecialType overload:

generator.TypeExpression(SpecialType.System_Double)
SLaks
  • 868,454
  • 176
  • 1,908
  • 1,964
  • The thing is that I am getting the type information from an ITypeSymbol and if I do `generator.GenericName("IEnumerable", typeSymbol)` and the typeSymbol happens to be a primitive (double for example) then the generic created will be `IEnumerable`. To clean this up so I just get back `double`, I tried to do `generator.GenericName("IEnumerable", generator.IdentifierName(typeSymbol.ToDisplayString(SymbolDisplayFormat.MinimallyQualifiedFormat)))`, but this escapes built in keywords. – Leor Greenberger May 03 '17 at 18:49
  • So how do I write this so that the type printed uses the keyword of a built in type or the minimally qualified format of a custom type? Thanks! – Leor Greenberger May 03 '17 at 18:50
  • I edited the original post to ask if my proposed way to do this will work. – Leor Greenberger May 03 '17 at 19:01
  • 1
    @LGreenberger: You should just use the non-primitive typenames only, then use the Simplifier to automatically simplify. – SLaks May 03 '17 at 21:51