5

I want to automate the Visual Studio 2010 / Resharper 5 auto inserting import directives to put my internal namespaces into the namespace sphere. Like this:

using System;
using System.Collections.Generic;
using System.Linq;
using StructureMap;
using MyProject.Core;          // <--- Move inside.
using MyProject.Core.Common;   // <--- Move inside.

namespace MyProject.DependencyResolution
{
    using Core;
    using Core.Common;   // <--- My internal namespaces to be here!

    public class DependencyRegistrar
    {
        ...........
    }
}

Currently, I'm doing it manually, the problem is that with every refactoring the using directives going up, to the beginning of the page.

Fitzchak Yitzchaki
  • 9,095
  • 12
  • 56
  • 96
  • 2
    Just curious.. why? You shouldn't have more than one class in a file in any case.... – Billy ONeal Apr 30 '10 at 05:26
  • Because it's make the code more readable... at least for me. – Fitzchak Yitzchaki Apr 30 '10 at 05:34
  • There is also a couple other valid reason to want to do this. Type hiding/overriding - The compiler will favor the type located in the inner namespace over the same-named type in the outer. This can be useful when you have specialized a type in your custom assembly and don't want to have to resort to aliasing in order to resolve conflicting references in your code. Also extension method aggregation, meaning extension methods specified in inner namespace are added to those in outer namespace. – ricardo Nov 01 '13 at 13:27

3 Answers3

3

In R# 5.0:

ReSharper->Tools->Cleanup Code. Or simply press Ctrl+E, Ctrl+C.

Then use profile that has "Optimize 'using' directives" turned on.

Vadim
  • 21,044
  • 18
  • 65
  • 101
0

There is no option to achieve that. So probably the best action go with is a convention that you can achieve easily.

Fitzchak Yitzchaki
  • 9,095
  • 12
  • 56
  • 96
0

I think readability is better served if the statements are either (all) outside the namespace declaration, or (all) inside of it.

Among the using statements, sorting them with the project statements last (as per your example code) is then preferred.

Resharper follows both the above conventions, so I would recommend sticking to those :)

Martin Suchanek
  • 3,006
  • 6
  • 31
  • 31