Automatically add imports with ReSharper templates

135 views Asked by At

I am writing live templates for ReSharper that rely on types defined in an external namespace.

Is there a way to tell it "Add a using directive if needed", so that I don't have to fix that manually after each use

/* Template expands to */
var $ListName$ = new List<$Type$>()$END$;

/* But sometimes needs to import */
using System.Collections.Generic;
2

There are 2 answers

2
ulrichb On BEST ANSWER

Yes, this is possible.

Fully qualify your type names, and select "Shorten qualified references".

Live templates

0
citizenmatt On

Yep, in your template, use the fully qualified type name, e.g.

var $ListName$ = new System.Collections.Generic.List<$Type$>();

If you then check "Shorten qualified references", ReSharper will insert the text as just new List<…>(); and automatically add using System.Collections.Generic; if it's not already there.