Software to group c# class members - c#

Is there some software for visual studio to automatically group c# class members?

Resharper does that, amongst loads of other things.

Regionerate can do that, and I think GhostDoc too (not sure). Both Visual Studio add-ons are free.

I wrote this plugin for Visual Studio that displays a grouped view. You can choose what groupings to apply, what order they're applied in, and write your own.
http://ora.codeplex.com/
It has some issues if you ever use VB.NET, but some people have posted workarounds.
If you have a live-updated view like this, you don't need to manually maintain the ordering in the file, or spread a lot of silly nested regions around in your source. Regions (like comments) shouldn't be used to redundantly repeat information that is already in the code. It just gets out of sync.

http://www.usysware.com/dpack/
DPack includes various browser tools that allow the developer to quickly find solution files, types, particular type members, methods or properties for instance, or quickly find and reference standard .NET framework types.

Related

How to view source code of dependencies in Visual Studio 2015 by clicking F12?

Clicking F12 on functions from some of dependencies always results with some, often useless, list of variables and methods of class:
It of course provides some brief explanation/summary of what code does, but I often need some more details, preferably: see the code.
Since ASP.net is open-source now, I wonder if there was some possibility to view actual source code of class (using F12 button) instead of showing this metadata. Is there some plugin for that?
I've found some interesting Visual Studio extension. It basically do what I wanted.
Ref12
It unfortunately doesn't support every .NET classess (especially those from the new asp.net core seems to be missing), but it will definitely be useful either way.

API for C#/.NET code analysis

I'm not sure if the title is accurately describing what I'm trying to ask...
Basically, how does Visual Studio look at a code file in a CSPROJ and determine references for a method or variable, or if a using statement is not being used in the code? Obviously it's not just doing simple text parsing, and it seems to work before ever doing a build so I don't think it's referencing the assembly/CLR.
Is there an API that I can tie into from an external app, if I have the user select their CSPROJ or SLN file and be able to report on unused using statements in files, or methods/vars that aren't being called anywhere in their code?
I'm asking because I'm considering building some sort of reporting tool to show areas where code cleanup could occur, but I'm not really sure where to begin.
Thanks!
EDIT: As a followup question, is there a similar tool already out there?
Visual Studio does compilation on the fly, in order to determine many of the things like this, in addition to providing functionality like Intellisense.
Right now, the compilers are all a "black box" and not directly usable. The Roslyn project has the goal of changing this, allowing the full breadth of code analysis being done to be usable. It's currently in a CTP, and could be used now.
EDIT: As a followup question, is there a similar tool already out there?
Many third party extensions, such as Resharper, provide much of this functionality (such as their Safe Delete refactoring and Solution Wide Inspections).

Roslyn vs Reflection for TypeScript code generator

I'm developing a TypeScript code generator that will use custom attributes on C# classes to generate TypeScript definitions and code files.
I'm considering two options for TypeScript code generation / source file analysis:
Reflection on compiled assemblies
Roslyn CTP
The tool would use custom attributes on properties and methods to generate a TypeScript file. Right now I'm not planning to convert the C# method body to JavaScript, but in the future this may be done. So for this reason I am seriously considering Roslyn. However to simply generate the outline of my TypeScript classes I think I could use reflection and custom attributes.
I am wondering:
a) Does Roslyn provide functionality that is impossible with Reflection? My understanding is that I cannot get method bodies with Reflection.
b) Would the Roslyn CTP license prevent my from distributing the tool under an open source license? This is not clear to me after reading the license
I just did something along these lines - works great for creating your datamodel in Typescript from your c# classes. I built it to generate a single AMD-module with an interface which mimics the basic data of your Models. Also supports Generics, and creates a class with Knockout properties, including a toJS() method and an update(data:Interface) method to update your class.
The whole thing is just a single T4 template. If anyone finds this and is interested: http://spabuilder.wordpress.com/2014/07/31/generating-typescript-from-c/
Also honors [KeyAttribute] and [Timespan] attributes for data models if you are using data annotations.
I've been messing around with generating js, and I'm finding Reflection to be a better tool for this. I'm basically pointing my generator at the bin folder of the project which the metadata comes from. There might be some difficulties with loading all the needed assemblies, and caveats with versions of assemblies in the bin folder, and versions of the same assemblies that your generator project references. But once you get over all of this, which I did with minimal difficulty, Reflection is a lot easier to use, and more reliable.
With Roslyn, you are basically just parsing c#. Roslyn does this very well, but I'm hesitant to switch to it from Reflection. With reflection, you get metadata more reliably.
Let's say you want the Prefix property of a RoutePrefixAttribute that decorates a controller class. If you're parsing c#, you may have:
[RoutePrefix("stringliteral")] or [RoutePrefix(constantString)]. So, you have to worry about whether it's a literal or a constant expression, then find out how to get the value of a constant expression, worry about all the different ways in which you can pass parameters to an atatribute (for example, will this break your code: [RoutePrefix(Prefix="literal")]...
Once you're dealing with the actual runtime objects with reflection, everything is just easier. You have a nice RoutePrefixAttribute object, and you can go routePrefix.Prefix to get, reliably, the value of the prefix.
This is just one example of how doing things with Reflection is easier. It's the difference between gathering metadata from a set of c# objects in a type-safe way, and scraping data from c# code, albeit with a really nice scraping tool.
EDIT: Since writing this answer, I've bit the bullet and switched to Roslyn. It's fairly powerful once you get the hang of it, and I did find one big advantage: you can get a reference to the workspace from a visual studio plugin, and easily do all kinds of stuff within the plugin.
Update Nov, 2018
The accepted answer is valid because it's dated in Aprl,2013
Now roslyn is distributed under Apache License Version 2.0
excerpt from the license:
Redistribution.
You may reproduce and distribute copies of the
Work or Derivative Works thereof in any medium, with or without
modifications, and in Source or Object form, provided that You meet the following conditions:...
Roslyn have a number of nuget packages
Doesn't the license only forbid you personally from distributing the binaries? It doesn't forbid you from adding a dependency from your NuGet package to the Rosyln CTP NuGet package. You personally cannot deliver the bits, but you can have NuGet pull in Roslyn automatically.
So just avoid checking Rosyln source or binaries into your version control.
The Roslyn website not clearly states that:
The current license is for evaluation and preview purposes only and
does not allow redistribution of the Roslyn binaries. Sharing of
sample projects built on the Roslyn APIs is permitted, but sample
users must have either the Roslyn CTP or the Roslyn NuGet package
installed in order to build and run.
I wouldn't use the current Roslyn CTP - simply because there will be new versions in 2014 and those will bring many breaking changes for sure. So you might end up with totally deprecated code.
(There recently was a blog post on this by a MS team member, but I'm afraid I currently don't have the link at hand.)
EditThere's a good chance that Roslyn then will get a license that also permits for commercial use...
Update - July 2015
Roslyn is still in CTP, but their FAQ on GitHub is much more to the point:
For sample code or learning purposes, the recommended way to redistribute the Roslyn DLLs is with the Roslyn NuGet package: [url:Microsoft.CodeAnalysis|http://www.nuget.org/packages/Microsoft.CodeAnalysis].
So it appears that you still cannot redistribute the DLLs in finished products. The project will need to be open sourced and the solution will need a reference the NuGet package.
Original Answer (November 2012)
I don't believe you can distribute under open source.
6.DISTRIBUTABLE CODE. The software contains code that you are permitted to distribute in programs you develop if you comply with the
terms below.
6.c Distribution Restrictions you may not modify or distribute the source code of any Distributable Code so that any part of it becomes
subject to an Excluded License. An Excluded License is one that
requires, as a condition of use, modification or distribution,
the code be disclosed or distributed in source code form; or item
others have the right to modify it.
At first it sounds like you could do it if you just include the Roslyn binaries, but the Distributable Code definition specifically says "The software contains code..." and I believe that is what everything after is referring to.
To your other question, Roslyn isn't fully finished and is still Beta. I don't know exactly if it is currently in a state that allows it to handle your needs. That's something you may just want to spend a couple of hours tinkering with. I wouldn't think it had more functionality than what .NET currently allows. You can see what they recently added in September here and what is currently not implemented here.
For my experience using T4 generations based on reflection, as TypeLite does, is somehow simpler but has some drawbacks, like once the project depends on the classes that have been generated, regenerating them with a breaking change (renamed a class) will lead to a non compiling project so running the template again will output a blanck file and the user will have an hard time making everything compile again.
So, having the same need, i started experimenting with Roslyn, and it seems very promising, but i have many doubts on how to use it properly...
You can take a look at what i'm doing and maybe help me here: https://github.com/TrabacchinLuigi/RoslynExporter

Find all objects that are not used in VS 2010 Pro so they can be safely removed?

I am trying to go through inherited code and find find out what objects are not being used anywhere so I can safely delete them. Is there a tool in VS 2010 Pro that enables this? I have been just doing a Find, but I find that this at times may be misleading.
JetBrains ReSharper can do this for you. You'll still need to be intelligent about removing classes, though. If you're using and DI containers or configuration through external files I've seen it think things are not used, when in fact they are.
Here's an example of how it reports this in a file (there's also solution-wide analysis that can be run):
Resharper has this feature - if you turn on solution wide analysis, any member of a class that is not used will have its name grayed out.
You do need to be careful if you use an IoC container of some sort, as it may be using a member at runtime but static analysis of the code base will not be able to discover this usage.

Rearranging a class's methods, properties and variables

In Visual Studio 2008, is it possible to rearrange a C# file automatically based on methods, properties or variables?
I could do this by manually, but was wondering if there is something that will automatically do the arranging. Basically I would like some kind of a grouping mechanism.
Not natively. Have a look at ReSharper by JetBrains. It's not free, but more than pays for itself in a little over 5 minutes of usage (depending on how slow you type).
EDIT:
In response to comments, I must qualify that reordering and group code blocks in ReSharper requires the StyleCop for ReSharper plugin. It is a free, open source plugin for ReSharper and is available on Codeplex.
There is a free plugin called Regionerate. I personally don't like it but it does what you want.
You could also look at CodeRush Xpress for C# http://msdn.microsoft.com/en-us/vcsharp/ee663901.aspx it is free and has refactoring tools included.
Or by chance if you are using "DevExpress" in your projects, it comes with Refactoring utility which you can make use of.

Categories

Resources