1

There is an interesting coding standard in my shop that says that we don't shortcut type names with a using statement, they have to be fully qualified (so any time you reference a type, you use MyRootNamespace.ANamespace.MaybeAnotherNamespace.MyClassName, instead of just "MyClassName").

Love it or hate it, that's just how we roll, and I can't do anything about that.

Of course, you are swimming upstream with Visual Studio, because all the editing tools that generate code for you (member completion, event completion, etc) use the short type names wherever possible.

What I would like to do is to build some sort of extension or macro for Visual Studio that will correct a partial declaration, and replace it with the fully-qualified typename.

I started out trying to build a macro that would run for the symbol that your cursor is on (though I'd like to be able to scan a whole file, or maybe just intercept the code as you type).

I found that I can get members of a class with FileCodeModel2.GetElementFromPoint(), but this method will only work on class members -- it won't pick up a variable declaration inside of a method, for example.

Is there some other way I can get at that stuff? I'm currently using Macros, but would the new VS Extension model be more appropriate? I'm using Visual Studio 2010 Ultimate edition, and I only need to target c# code.

JMarsch
  • 21,484
  • 15
  • 77
  • 125
  • Most programming shops don't pay by the character. ;) However, if you were using ReSharper, it includes options for this sort of thing so that you can always use FQ type names. – Kirk Woll Aug 26 '10 at 15:30
  • 1
    Wow, that's a very backwards coding standard. – JaredPar Aug 26 '10 at 15:56
  • @JaredPar on the one hand, I agree with you, and on the other, I have to say that you do kinda get used to it. The code is wordier, but when you look at a type def, you know exactly where it's coming from. I realized one day that I had sort of "crossed over" when I was cursing a code example -- there were typerefs, but no info on references or what using clauses were needed -- I was actually wishing the refs were fully qualified! All in all, there are reasons to hate or like it. I'm pretty neutral. – JMarsch Aug 26 '10 at 22:21
  • @Kirk Woll: I had been thinking about having a look at resharper anyway, maybe this will push me over the edge. – JMarsch Aug 26 '10 at 22:22

1 Answers1

1

The object model allows you to do many things that are available within the IDE. But as such a feature is not available in the IDE you are out of luck here, I'm afraid.

The only thing you can do automatically with the using directives is sort them and remove the ones that are not used.

Update

As it seems it is somewhat possible to retrieve a fully qualified type name from a macro. However, it seems problematic with special cases such as generics.

Community
  • 1
  • 1
Dirk Vollmar
  • 172,527
  • 53
  • 255
  • 316