使用 ReSharper 模板自动添加导入

Automatically add imports with ReSharper templates

我正在为 ReSharper 编写依赖于外部命名空间中定义的类型的实时模板。

有没有办法告诉它"Add a using directive if needed",这样我就不必在每次使用后手动修复

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

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

是的,这是可能的。

完全限定您的类型名称,select "Shorten qualified references"。

是的,在您的模板中,使用完全限定的类型名称,例如

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

如果您随后选中 "Shorten qualified references",ReSharper 将插入文本 new List<…>(); 并自动添加 using System.Collections.Generic;(如果不存在)。