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?

1 Answer 1

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 =(

Sign up to request clarification or add additional context in comments.

1 Comment

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.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.