Visual Studio add-in for C#-development - c#

I want to write my first Visual Studio 2010 add-in. My aim is to create an add-in that helps to automatically generate code for things like:
implement superclass constructors
create delegation methods for composed object
etc.
Of course, in order to know what to generate, my add-in must have a knowledge of the code-file the user is going to insert generated code into. (classes, base-classes ...)
My first thought was to use a C#-parser. Is this the right way to go? Isn't there any .NET-Framework part, COM-Object or whatever "built-in"?
Thank you in advance.
P.S: Is noticed that CSharpCodeProvider's class Parse() method has not been implemented by Microsoft yet. -_-

You can get a lot of info from the MS Visual Studio Extensibility site. This post from the Visual Studio blog may also get you started with dealing with the in memory code file.

You could start by looking at EnvDTE, which will help you browse through project files and code trees already parsed and analyzed by Visual Studio, as well as modifying them. EnvDTE is pretty basic and error-prone (for the few times I used it). Finding good in-depth tutorials is also difficult.
If you're trying to implement such features yourself, good luck, it's definitely doable but the learning curve might be quite steep. Otherwise, you could have a look at ReSharper which already implements the features you're trying to code (and much more). Plus it also has a very well designed API compared to EnvDTE (although not very well documented), allowing to use its internal C# parser / analyzer.

Related

Fast easy refactoring in visual studio

I have a large project that is quite a mess. There's God classes, poor variable naming, poor indentation and spacing...among other issues.
I'm looking for a quick and easy way to make marketable improvements to such a project. In Visual Studio 2010 CTRL + K and CTRL + D gives me fast code cleanup. Organize usings takes out unused namespaces. But, both only seem to work on 1 page. Can I apply these changes to a project? Are there any similar cleanup techniques or free and reliable 3rd party addons?
Try out Resharper-- it can do what you're looking for.
One third party extension that I use that is free is DevExpress' Coderush Xpress (For C# and VB), which is the free edition of their full product. The great thing about this is that the express edition still lets you add CodeRush plugins to it (in addition to several project-wide refactoring options), which opens you up to a ton more options than just the Visual Studio Plugins.
Here is a huge list of the plugins available for CR Xpress.
I have been using Visual Assist X for quite some time, and I found it very useful. It is not free, and it may not automate the sweeping changes you seem to be looking for, but it makes simple refactoring much easier.
P.S. I have been using it for C++, not C#. But if anything, I would assume that it would have more capabilities for C#.

Does Visual Studio for C# have these Eclipse for Java features?

So far I use to develop in Java. Java is multi-platform (now works on Android!), has a very powerfull VM and is open, well behaved, etc. But is also old and seems to be stopped on time in terms of language features. Scala and Gosu are nice replacements, but I don't like Scala syntax and Gosu is very immature and unlike to win from Scala. All this makes me think about moving to C# at least for web development! Phew!
One thing that is quite important to me is IDE support. Right now I use Eclipse for Java, and my favorite features are these (most important first, somewhat):
Full code navigation (call hierarchy, show variable reads & writes, inherited members).
Incremental compilation (which means fast compilation).
Many kinds of errors are detected and underlined before compilation.
Many intelligent quick-fixes (can fix/write many code for you and quickly rename elements and refactor references).
Intelligent and configurable code completion. Display hints even for unimported packages/classes.
Over 15 kinds of refactorings, all of them very useful.
Over 15 options of source generation (add unimplemented methods, generate getters and setter, generate delegates).
Configurable code formatter, even for code fragments (select code then format).
Debugger supports hot code replacement and "Drop to Frame" so I can go back an check other things without full program startup.
Code cleanups (remove unnecessary parenthesis, remove unnecessary "this" references, etc).
Very decent, autonomous and seamless CVS integration, with integrated file comparison and computer-aided merge.
Very nice tools for web development (server deployment, JavaScript and HTML editor with formatter).
Tons of plugins (code coverage analyser, memory dump analyser, eGIT).
Which of these features are available in Visual Studio for C#/ASP.NET? If I can get some by adding a cheap plugin, please tell.
Well, I like Visual Studio a lot more than Eclipse. I have only used Eclipse for minimal Java programming and Action Script. Visual Studio can do every thing you listed and if you combine it with a paid plugin like Resharper or CodeRush, you get a lot more. Why don't you download Visual Studio Express and play around with it? That would be much better than getting an answer from a very biased C# dev.
VS2010 with the addition of ReSharper has most of these things. I can't speak to CVS, but it does have fine SVN integration.
I started using VS about six months ago after a few years of Eclipse, and it works pretty well.
Nothing is cheap in the VS world compared to Eclipse.

C# SDK for non-IDE Java developer

I've been playing with Java for years as a means of developing quick and easy tools for repetitive tasks. I am not a true "developer" in the traditional sense, but I have lots of experience creating a wide assortment of tools and PoCs.
Unfortunately for me, I have noticed many shops are specifically looking for experience with C#, and not so many for Java. Even here on SO, there are more questions and more followers to C# related issues than Java. My preference will always be Java over C# simply for the cross-platform compatibility, but since the languages are so similar, I believe it would be beneficial for me to cross-train. I have already dabbled in other languages and scripts (VB and other BASIC flavors, Javascript, VBScript, ASP, JSP, PHP, etc.) so adding another isn't out of the question.
My current Java environment simply consists of a text editor (primarily jEdit for its plugins and layout) and homemade scripts to compile/jar my projects. I don't like to use IDEs because I want full control over my code and don't want a program writing code for me. (I also prefer to write my own code as opposed to using any sort of external library/package, if feasible. It helps me learn and greatly reduces unnecessary code.)
Therefore, what are my options for a non-IDE C# SDK? Libraries are obviously not that important to me. I've heard of Mono, which appears to separate the functions, but haven't tried it yet. What other SDKs exist that are similar to a simple Java SDK combined with a text editor?
Using an IDE doesn't have to mean anything writing code for you. I'm not generally keen on designer-generated code, but unless you decide to use a designer (or snippets etc), Visual Studio won't be writing code for you. Of course it will create a skeleton class for you when you create one, add the appropriate method signatures when you implement an interface etc - but is that boilerplate really something you want to write yourself?
I'd also suggest that your policy of not using external libraries is a bad one. I agree that it's useful for educational purposes to sometimes reimplement something, but it means that the code quality is likely to end up being worse... why would you not want to use code that has already been used and improved by many other people? Yes, you need to be careful about what libraries you use - but you really don't want to do everything yourself... not if you want to be productive, anyway.
I often use a text editor and command line myself for simple test code (e.g. for Stack Overflow questions) but I wouldn't dream of doing that for "real" code that I plan to keep.
Honestly, a lot of C# and .NET is about learning the tools; Visual Studio gives you a lot that you wouldn't be able to do with a text editor. There's a free version, and I highly suggest you check it out! People hiring will want to know that you're familiar with the tools they'll most likely be using.
You can just start with Notepad and csc.exe, the the command-line C# compiler that ships with the .NET SDK.
However, IDE is not necessarily synonym for code generator. I would download Visual Studio Express and start with empty Console projects.
Have fun!
Don't forget you can build/assemble C# projects using MSBuild and a .sln file if you really want. But the IDEs will make life a lot easier.
The IDE will not write code for you, it will help you writing code. Using libraries will help you concentrate on what you really want to program, not the things that already have been done.
Check into AvalonEdit, the text editor component of SharpDevelop. It is an open source text editor that has classes that could implement features such as intellisense and syntax highlighting. You would only have to use as much of it as you wanted and you could embed it anywhere you would use a text box control.
If you are familiar with Ant from Java then you could also check out NAnt to do your compile phase.
As far as I know, with Visual Studio you also get a command line C# compiler, csc. You could use a text editor and manually compile your C# code with that on the command line if you really want - that wouldn't be very practical however when your project contains more than a handful of source files.
If your project becomes bigger, you could use a tool like NAnt, which is a .NET version of the popular Java build tool Ant.
I agree with Jon Skeet about that your way of working is not very practical. If you are really looking for a software development job, you'd better learn to use the tools that other developers use. An employer will also not accept the fact that you'd want to write all the code yourself instead of using libraries. By using libraries instead of writing it all yourself you save lots of time, you are reusing well-tested code and your code will be much easier to maintain by other developers.

Why doesn't my CreateRibbonExtensibilityObject method get called?

Hi I've been tinkering around with the two techniques to modify the ribbon with an MS Word addin (Visual Studio 2010, .NET 3.5), a Microsoft.Office.Tools.Ribbon.OfficeRibbon and Microsoft.Office.Core.IRibbonExtensibility. I was hoping to switch between the two in my addin's CreateRibbonExtensibilityObject, but for some reason it is not called now. The documentation says that if an addin implements CreateRibbonExtensibilityObject that it will be called. When I create a fresh project to test doing this very thing, it works. I'd prefer not to abandon my project as-is. Any ideas? Thank you.
They're not really interchangeable. The issue is that if you use the OfficeRibbon object (which is really pretty nice, it's got a nice designer etc) then IT implements the createribbonextensibility stuff and you don't have a chance to intercept it.
I did a blog post last year that discussed the problem with respect to adding support for the new Office 2010 "Backstage view" from a VSTO 3 addin (VSTO 3 doesn't support Backstage, but it's still doable, and you'd basically use the same technique to swap out implementations of the ribbon support).
the real question is, why bother? If you've done the work to support IExtensibility2 (and then have the ability to completely define your ribbon object in XML), there's not much of a need for the OfficeRibbon. and if you're using the officeribbon control, it can do jsut about everything you could do specifying the xml directly, so why suffer the pain?
Blog post here
http://www.vbfengshui.com/integrating-with-the-new-office-backstage-from-a-vsto-3-addin/

Is there anyway to trace all classes and interfaces used or referenced in a C# project?

I have a huge cryptography solution that has way more than what we need. I need some way of debugging the program and keeping track of each class that was called after running my main() method. Any tips? Right now I'm attepting to hold down F11 and go through all the classes that opened up one by one and manually check what was called. Not what I want to do. I'm stripping out everything that wasn't needed afterwards for memory leak, performance, and size reasons. It's open source code. Visual Studio 2008
Dottrace should do the trick for you. It has a really handy way of profiling any running .NET code and spitting out an easy-to-consume report. I highly recommend the tool (it's also good for what it's designed for - performance & memory profiling). This sounds like an ad, but I have nothing to do with the company. I just love the app.
You can use the .NET Reflector tool. Also this is a built-in feature in Visual Studio 2010.
Tracing has always been the default showcase for AOP.
PostSharp: http://www.sharpcrafters.com/aop.net

Categories