1

I am writing a ReSharper plugin and I want to do this:

CSharpElementFactory factory = CSharpElementFactory.GetInstance(treeNode.GetPsiModule());
factory.CreateTypeMemberDeclaration(
    "public static $0 $1 (this $2 $4) { }",
    "string",
    someMethodName, 
    someArgumentType,
    SomeArgumentName);

which I want to output the code:

public static string SomeMethodName(this SomeArgumentType someArgumentName) { }

but it actually outputs this:

public static @string SomeMethodName(this SomeArgumentType someArgumentName) { }

it seems to do this with int (and I assume other built in types or keywords).

How can I prevent it from doing this and outputting valid code?

ulrichb
  • 19,610
  • 8
  • 73
  • 87
Sam Holder
  • 32,535
  • 13
  • 101
  • 181

1 Answers1

1

if you'd like to use keyword 'string' you can't use quotations for it. Use 'string' in the pattern (otherwise ReSharper will try to escape 'string' keyword to use in the identifier position):

public static string $0 (this $1 $2){}

If you'd like to use 'System.String' you can explicitly bind '$0' to IDeclaredType for string:

IPsiModule module;
  factory.CreateTypeMemberDeclaration("public static $0 $1 (this $2 $4){}",module.GetPredefinedType().String,someMethodName, someArgumentType,SomeArgumentName);

ReSharper should follow code style in this case and automatically replace it with 'string' when code style is set to use keywords, but I'm not sure that it will work =(

Shkredov S.
  • 2,097
  • 16
  • 8
  • Thanks. I am not hardcoding the 'string', instead I'm getting from an `IPredefinedTypeUsage` from navigating the code tree. Basically I have a plugin which I want to create an extension method if a method is undefined on an instance, and I want to workout the return type that the method should take based on the usage in the code, and at this point I don't have an `IDeclaredType` I don't think. – Sam Holder Sep 23 '15 at 19:04