Using Visual Studio DTE to analyze source code - c#

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.

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.

Related

learning public methods of a class

I'm learning a bit of C# and I know a bit of c++ right now.
When I want to learn about a class's public methods - I look in the header file in C++- which C# doesn't have. Where do I get a good summary of a class's public methods without having to look in the source file?
In the documentation, in Visual Studio using the object browser or IntelliSense or using reflection (either at runtime or using third-party tools).
There are few ways:
1) Read the documentation - .NET Framework class library or the appropriate one for used library.
2) Take a look at class in Class view
3) Use some metadata reading tool like ILDasm
4) The last in the list, but the most useful and most used is Intellisense
C# doesn't have header files, but byte code-compiled assemblies (i.e. exe and dll files) contain a section with metadata about namespace and namespace members, which is used by the runtime to discover types, methods, properties and so on. Also, Visual Studio uses it to offer its powerful intellisense.
Anyway, if you press F12 when cursor is on a method name, or any member (class, enumeration, ...), or you right-click there, Visual Studio will go to the definition of the whole member, and if such member has no source code in your machine, it will show metadata only:

C# code generation (visual studio template or resharper addin)

I would like to generate some C# code based on existing code.
More precisely I need some mappers for existing enums as well as converters, unit-tests for them.
It would be a longer discussion why I've generate this code rather than go for a generic approach, but considering that I would like to generate some classes based on some enumeration types, what options would I have?
I am just thinking about visual studio extensions, some templates or maybe resharper addins but so far I haven't done anything like this...
I would appreciate any input of those of you who had previous experience with such a task.
ReSharper supports Live Templates as a means of generating code. You can create whatever code you like in there, with editable or linked hotspots to provide customisation points (e.g. current file name, class name, time, new guids, suggested variable names, etc). You can generate code snippets in existing files, surround existing code, or create new files. ReSharper 8 also introduces support for multi-file templates, creating more than one file at a time.
However, ReSharper's templates don't support things like loops - you can't loop over an XML file and generate a class member for each element, for example. T4 would be a better solution for that.
Reegenerator supports generating code from existing code. You can create a code generator and then attach it to the file that contains the enum type. The generator will be executed everytime the file is saved.
The code generator are given access to the Visual Studio DTE Object, which will give you the code structure through CodeElement classes (CodeClass, CodeAttribute, etc). From there you can either generate a separate partial file through a T4-like template, or simply manipulate the code directly using the DTE EditPoint.

Is it possible to mark a module function as "hidden" from Intellisense discovery?

As a follow up to this question, I am wondering whether it is possible to mark an F# function within a module (presumably via an attribute) as "hidden" from Intellisense. To recap, I have some functions which are marked inline but are implemented in terms of other functions I don't really want to expose. So while the implementation functions must be kept public (since they are being inlined), I remember that in the past I've come across C# methods which were hidden from Visual Studio Intellisense but compiled just fine if you knew what they were, but I don't remember the exact method(s) and am unsure if that was some sort of ad-hoc Visual Studio thing or a usable feature like DebuggerBrowsable(DebuggerBrowsableState.Never) awareness.
Update: I tried applying the EditorBrowsable(EditorBrowsableState.Never) but it doesn't appear to work in Visual F# projects.
I tried to do that recently and I'm pretty sure that F# ignores the EditorBrowsable attribute.
The only way to make declaration disappear from the IntelliSense is to use the ObsoleteAttribute, but that also means you'll get a warning when you actually use the function. This is a bit unfortunate, but it may be okay if you use the function only from some implementation file where you can disable the warning:
Declaration in one file:
module Internal =
[<System.ObsoleteAttribute>]
let foo = 10
Implementation file that disables warnings and uses foo:
// Disable 'obsolete' warning
#nowarn "44"
// 'Internal' is empty (and is not shown in the completion list)
Internal.foo
The attribute can be applied to modules, functions and types, so it is quite flexible.
You can use [EditorBrowsable(EditorBrowsableState.Never)], but this will only work for clients of your assembly that are not in the same solution (i.e. importing your class library as an assembly and not as a Project.) Projects in the same solution will still show the methods in Intellisense.

Is there a way to change the order of constructors listed in IntelliSense in Visual Studio?

I have defined a class with multiple constructors so that the underlying interfaces are immutable once the object is instantiated. I would like one of the constructors to be the "default" constructor for when a user types the following in Visual Studio:
var obj = new MyClass(
Dim obj As New MyClass(
Currently when I go to instantiate the object, the constructors aren't listed (in Visual Studio IntelliSense) in the order I declared them in my class. Is there a way to mark up my constructors so that their methods appear in a particular order during instantiation in Visual Studio IntelliSense?
There isn't a way to control the ordering within Visual Studio's Intellisense. If you do have multiple constructors (or methods), your only real control in terms of intellisense is to use EditorBrowsable with the appropriate EditorBrowsableState. This allows you to hide a contructor (or method) in intellisense, or only have it displayed in "advanced" mode, but does not allow you to reorder them.
However, in this situation, if you're targetting .NET 4, I'd recommend considering using a single constructor, and taking advantage of named and optional arguments.
That's an interesting question, but I haven't heard of such capability. One option would be marking the other constructors as advanced or hidden.
ComponentModel.EditorBrowsable( ComponentModel.EditorBrowsableState.Advanced )
ComponentModel.EditorBrowsable( ComponentModel.EditorBrowsableState.Never )
I'm pretty sure what you are asking for is impossible. The best thing to do is to mark up your constructors with XML comments which will be used to populate the intellisense in VS. That will give the user detailed intellisense about which constructors is default etc.
Editorial: I'm pretty sure VB.NET orders the constructors in the order of their present in the class declaration.
You have to remember that Intellisense isn't a feature of the language, but of the editor. You wouldn't have features specific to the IDE built into the language because other editors can be used to write code. Writing code for the purpose of writing code is off the mark.
You could always use a Factory model and make the constructor protected for each method. However I perfer Reed Copsey answer. However if .NET 4.0 is not an option this could be an alternative.
It looks like there still is no way to dictate the order as an application developer. It appears IntelliSense sorts first on the number of arguments, disregarding whether an argument is optional or not (it counts either way).
If you have optional arguments you can rework them to overloads though, thus creating a new method/constructor with less parameters which will then appear earlier. You can also hide constructors that are useless to an application developer by declaring them internal.
Using these methods I could fix my little problem. I had a deserializing constructor that popped up before the more commonly used one.

C#: Discovering Extension Methods

What tools or techniques do you recommend for discovering C# extension methods in code? They're in the right namespace, but may be in any file in the solution.
Specifically:
How can I find all extension methods (and navigate to them) on the current type in the code window?
I do have Resharper (v4), so if that has a mechanism I'm not aware of - please share!
If you have the source then you can search for this Type identifier using regular expressions. Considering the fact that it has to be the first parameter to the function something like this should do the trick:
\(this:b+:i:b+:i
At least this way you can discover where the extensions methods are defined and add that namespace, then rely on intellisense. Just ran this on a non-trivial project with lots of extensions methods everywhere and it worked a treat. The only false positive was something like this:
if(this is Blah...
Which we can fix by adding static to our search since the extension methods have to be static:
static.*\(this:b+:i:b+:i
This won't work for cases like this:
public
static ExtensionMethod(
this int
iVal) {
}
But that's kind of the limitation of regular expressions. I am sure certain somebodies can tell you all about the pain of using regular expressions to parse a language.
Now, what I think is missing from the IDE is the ability to recognise the extension methods that are in a non-imported namespace. Similar to when you know the classname, if you type it up, the IDE will give you a hint to either use it explicitly or import the namespace. After all, that's how I import all my namespaces and frequently find myself trying to do the same to extension methods.
This is pretty low-tech, but how about Ctrl-F searching for "this:b+MyClassName" ?
If you are using VS which I guess you are intellisense will show all the avialable extensionmethod for a given object for you (marked with a blue thingy added to the usual instance method icon). That list might differ from file to file (a mthod called aMethod might mean two different things in two different files) eventhough the object type is the same (which is based on the way extension methods are found)
If you've got resharper, just hold down the ctrl key and click on the method.
If you have installed the ILSpy extension in Visual Studio (I am using 2022) then you can:
Right click on the class/type and select -> Open code in ILSpy
In ILSpy right click on the type and select -> Analyze
In the Analyze window you will see a node "Extension methods" (if any exists, else no node is shown)

Categories

Resources