C# Using statement and template?
Answered
I have a snippet (live template) which adds the boilerplate to a class to instantiate its logger. This requires a using statement to be added to the file.
Is it possible to have the template add the using statement automatically? Right now, once the snippet is pasted in, it then gives me the usual click here or press ctrl-Alt to add the using statement, but that is more clicks or keystrokes.
I tried searching any information on this, but couldn't find an answer--the keywords are very common and make it hard to get a clean search result.
Thanks very much.
Please sign in to leave a comment.
Hello Scott,
You need to specify a fully qualified namespace instead of the short one and tick "Shorten qualified references" checkbox for the template. Then after inserting the template, ReSharper will insert needed using to the file.
e.g. "var $Name$ = new System.Collections.Generic.List<int>();" template will be transformed to "var someName = new List<int>();" code and "using System.Collections.Generic;" will be inserted to the top of the file.
Thanks!
Did you try to check the "Reformat" checkbox. Maybe this will do the trick (didn't tried it myself).
Regards
Andreas
Thanks Andreas, having the `Reformat` checkbox ticked and complete type names (including the namespace) does the trick.
But what about invoking an extension method that requires a using statement in a live template?
The closest I got is to stick an $END$ placeholder just after the name of the extension method so that after injecting the live template I can type ALT+ENTER to transform the extension into its "expression method invocation" form.
With `Reformat code` enabled, this renders to
and adds `using Namespace.To;` to the top of my C# file.