简体   繁体   English

使用Visual Studio DTE分析源代码

[英]Using Visual Studio DTE to analyze source code

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 "). 我的商店中有一个有趣的编码标准,它说我们不能使用using语句来缩短类型名称,它们必须是完全限定的(因此, 每次引用类型时, 都应使用MyRootNamespace.ANamespace.MaybeAnotherNamespace.MyClassName 。只是“ 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. 当然,您正在Visual Studio上游,因为所有为您生成代码的编辑工具(成员完成,事件完成等)都尽可能使用短类型名称。

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. 我想做的是为Visual Studio构建某种扩展名或宏,该扩展名或宏将更正部分声明,并将其替换为完全限定的类型名。

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. 我发现可以使用FileCodeModel2.GetElementFromPoint()获取类的成员,但是该方法仅适用于类成员-例如,它不会在方法内部获取变量声明。

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? 我目前正在使用宏,但是新的VS扩展模型会更合适吗? I'm using Visual Studio 2010 Ultimate edition, and I only need to target c# code. 我使用的是Visual Studio 2010 Ultimate版,我只需要定位c#代码。

The object model allows you to do many things that are available within the IDE. 对象模型允许您执行IDE中可用的许多操作。 But as such a feature is not available in the IDE you are out of luck here, I'm afraid. 但是,恐怕由于IDE中不提供此功能,因此您很不走运。

The only thing you can do automatically with the using directives is sort them and remove the ones that are not used. using指令可以自动执行的唯一操作是对它们进行排序,然后删除不using指令。

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. 但是,对于诸如泛型之类的特殊情况,这似乎是有问题的。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM