I've been working on learning Roslyn and have made great progress with using the CSharpSyntaxRewriter mechanism to work on code. My goal in the end is to have a customizable coding standards enforcer. As such, I started with Microsoft's code formatter project from https://github.com/dotnet/codeformatter/releases. Right now what I'm working on is formatting white space.
The goal here is to learn Roslyn, so I want to write the code to format all of the whitespace instead of using the Formatter built into Visual Studio and Roslyn.
Formatter.FormatAsync(document, cancellationToken: cancellationToken);
I'm able to parse syntax trees and know I can implement the code necessary to do this using the CSharpSyntaxRewriter, but what I'd like to do is somehow simply get the raw source text for the code, do whatever manipulations are necessary character by character in the source file, and then put the new text back as the document.
However, I cannot figure out if it's even possible to do what I'm trying to do with Roslyn/Visual Studio. I would have thought so, since the source is simply a text file that's already loaded into Visual Studio, and the 'document' object can be acquired for it.
Edit
The optimum solution would be a drop down (or context) menu for C# source files that then ran all modifications on the file. I was unable to link MEF/MPF to any sort of hook that would allow whole-scale modifications of a source file. As such, I went to a console application.
From what I understand, MEF/MPF can provide single entry points to the source file, whereas the Roslyn interface allows simultaneous access to the entire source file at one time. I believe that's why Microsoft used Roslyn in order to implement their coding standards enforcer.
As I said, I've only been looking at Roslyn for about a month, so I may be missing something obvious.
My boss set our IDEs up to use non-standard formatting. For example, it adds spaces between the parenthesis and parameters. This is fine for the new stuff that we've started from scratch, but it's super annoying when I go into existing projects and try to format the code with Ctrl-k-d because when I go to compare the file with the latest version before checking in my changes it looks like I've changed almost every single line in the file and it's difficult to pick out the actual changes.
I would like to be able to use his settings in the newer development, but switch to the default settings when working with other projects. Is there a way to accomplish this without having to go into the settings and uncheck/recheck all the appropriate options every time?
EDIT: I understand it might be good to reformat all legacy code to use the new standards. Unfortunately, that's not my decision to make. And even so, I sometimes work from my personal computer which I also use on occasion to do contract work for other companies. I obviously can't reformat their code to fit my bosses standards.
You can easily switch between settings, by creating separate .settings files and using a VSIX package to switch between them by loading them into Visual Studio - I use this for switching between Dual and Single monitor settings via menu items in the Tools menu.
Using an extension is annoyingly complex for such a simple operation, and this was a whole lot easier when Visual Studio supported macros. However I've uploaded my homebrew extension to GitHub in case you want to have a look.
The tricky bit is editing the settings files to only contain the ones you're interested in, which I expect are the ones in Text Editor\C#\Formatting\Spacing.
The Rebracer extension stores code formatting options alongside each solution and when you open a solution, Rebracer will automatically apply that solution's settings.
I have modified my question after the comment which refers to article.
I have a c# Win forms app which contains some forms and dialog boxes/message boxes for user interaction.
I want to localize it to a set of languages.
Most forms are created through the forms designer and the text/strings are set via the properties
Strings for message boxes and other runtime messages come from the code.
I have read the article here
http://msdn.microsoft.com/en-us/library/y99d1cd3(v=vs.71).aspx
Which points to 2 approaches,
First is to have Visual Studio create resouece files for you - this is done for forms designer gernerated code
The second is to manually add resource files and access them code using GetString("strMessage") method
The problem I have in my mind is for desingner generated code I have to use first approch in which VS will create resource files by itself.
To localize my run time messages and mssage boxes I have to use the second approach which means I have to manually add resource files.
I will end up in two sets of resources files for each language. Can I not use just one method and have both form of strings localized in one place ?
Thanks,
Unfortunately you can't (easily) have your cake and eat it too. It's just how MSFT designed things. You can't add message strings to the code-behind ".resx" files generated by the VS forms designer since those ".resx" files will be re-generated by VS any time you make changes to them (overwriting your message strings). VS even warns you about this if you try to add your own resources to these files (using the ".resx" editor in VS). And the converse isn't true either, i.e., you can't add the code-behind ".resx" resources to your manually created ".resx" files (without enormous difficulties). The situation really isn't that bad though. It's been done for years this way and it works. While I would personally prefer a central repository for all resources, every system has its pros and cons. BTW, you should use "strongly typed" resources for your message strings and any other resources in your ".resx" files (the ones you manually add yourself). Don't use "GetString()" IOW (or whatever other function you might find). Strongly typed resources are much easier. Google for the details and after learning how to use them (not hard), also search for the Strongly Typed Resource Generator by Dmytro Kryvko (a very useful tool for VS that improves things even more).
There is nothing stopping you using a combination of both techniques. Go through step 1 to create all of the language resource files, and then add your own strings to those files.
I am looking for a way to extract a nested inner class so that it is in a separate file. Is there an easy way to do this in Visual Studio 2008 (eg, using the refactor menu)?
EDIT
Maybe a refactoring menu option isn't necessary if manual refactoring is simple enough.
if i understand correctly, you can use partial classes
http://msdn.microsoft.com/en-us/library/wa80x488(VS.80).aspx
I don't remember there being a built in refactoring for that, but it is available in Resharper. If you don't have Resharper and have a lot of them you need to refactor, you could always download and use the trial.
DevExpress offers a free coding assistance tool called CodeRush Express which offers this refactoring. Here's a link to the page showing the refactoring.
I own a license to their full-blown CodeRush w/ Refactor Pro, and I love it.
EDIT
Following the edit of the question, I just wanted to add that I'm able to do that refactoring using the following keystrokes (with the cursor on the name of the nested class)
Ctrl+` (opens refactor menu)
Enter (or arrowing down to the option if it's not already selected)
That automatically creates a new file in the project with the name of the nested class as the name of the file, moves the nested class to that file, removes it from the original location, and drops a marker at the place where the nested class was removed. Hitting Esc takes you back to where the extracted class was removed.
It is a huge time-saver. Granted, my day's not full of extracting nested classes, but it's an example of how saving a couple of minutes here and there can really help remove the friction from some common development tasks.
This question is a bit outdated, but I found a very good solution since Visual Studio 2017:
If you have a nested class, place your cursor on the class name. A little screwdriver appears on the left side (alternative press CTRL + .).
In the context menu you can select "move to myClass.cs".
Now it will refactor your nested class in a seperate file with all namespace changes.
Here is how it looks:
I am trying to learn Windows.Forms, and while I have Visual Studio (edit, my mistake obviously), I feel that I learn much more effectively by doing everything in Notepad. I have searched everywhere for a tutorial that takes this approach. I finally got started with http://www.geekpedia.com/tutorial33_Windows-forms-programming.html, but after the first example, it too begins working with multiple files? somehow generated with Visual Studio. How can I do this in Notepad? Can anyone point me somewhere helpful?
Thanks!
**While the overwhelming response seems seems strongly against this and I started to be convinced, I saw SmokingRope talking about it being as simple as writing all classes into one file. This is what I have been doing up till now for sizable, just non Windows.Form projects into. Can you maybe help explain how those necessary files can be included using this method?*
Seriously... I admire your fire, but you are out of your mind! What you can possibly hope to learn about .NET with NotePad that you couldn't learn a zillion times faster with Visual Studio? It's like asking, "I want to learn to drive a car. I have put wheels on my couch. What should I do next?"
I mean no disrespect, but make friends with Visual Studio and get busy learning. You're wasting your time otherwise.
It is actually quite easy to code C# outside of the IDE. It will basically come down to using the csc.exe compiler from the command line prompt.
The hardest part of this process is making sure the environment variables are correctly configure and Microsoft provides the vsvars32.bat batch file to do exactly that.
Here is a short tutorial on how to do use this batch file and the compiler from the command line: http://www.zeusedit.com/forum/viewtopic.php?t=1235
And an even better, but slightly more complicate approach is to use MsBuild utility to manage the building of the project: http://www.zeusedit.com/forum/viewtopic.php?t=2518
Finally you can even use the external C# Debugger to debug you programs: http://www.zeusedit.com/forum/viewtopic.php?t=2507
Your best approach is really to learn how to write code from within Visual Studio. You gain a lot of coding assistance (IntelliSense, syntax checking, etc.) that will help you learn the language.
If you really want to use Notepad, then you create as few or as many files as you want and then compile them in to an assembly using the command line compiler by listing each file as an input to the compiler.
The reality of this is that using notepad and the command line compiler is possible but very cumbersome and almost never used outside of showing simple "Hello, world" type examples.
I am going against the grain here... But I think that your idea is not such a bad one... especially for a small project.
But using Notepad (or at LEAST use Notepad++) will teach you more about MSBuild and the VBC or CSC compiler syntax than it will teach you about the language features. The best way to learn the language, is, as other have said, using Visual Studio. The intellisense is a great way to learn.
But it also makes us lazy and it is true that we don't have to memorize as much...and sometimes having things memorized comes in handy. (ie.... I am at a customer and logged in remotely to the servers... no visual studio is installed... BUT... yippee... .NET 2 is there... at that moment you will have appreciated the exercise...)
Honestly, to do this for a reasonably small project I think would be a good exercise in learning. I say go for it. Hell, I might even join you.
But, that said, I think the very best way to do it would be to use both methods side-by-side. For example... If you want to use multiple files the way to do that is to create a folder and put an vbproj (or csproj) file in it. MSBuild.exe receives vbproj files (and sln files for that matter) as arguments. So, one of the quickest ways to learn the vbproj file syntax is to use visual studio to create a project... and then open the vbproj file in Notepad++.
The problem is that Visual Studio IS SO AWESOME BECAUSE it abstracts so much away from the developer. But it is silly to not acknowledge there is a downside to that. That abstraction means that we don't need to have as deep an understanding. Things just work automagically. So I say dig a little deeper. I don't think you will regret it.
Assuming that the thing you want to avoid is magically-generated-code and visual designers, you can just open the System.Windows.Forms namespace and start coding against the APIs. Start with that first example, and then programmatically add buttons and textboxes and whatnot. You don't have to create a forms project or work with the designers, you can just 'write code' in VS and turn off all the magic.
If you're looking for example code, you might consider looking at F# samples, e.g. the UI stuff at
http://code.msdn.microsoft.com/fsharpsamples
for ideas of a few basic controls you can add to forms to do basic UI stuff.
You can write multiple classes in a single C# file (despite the generally accepted best-practice of putting one class per file):
using System;
namespace Test{
class Class1{ }
class Class2{ }
}
You also could look into how the MSBuild system works from the commandline. I believe you will still have to maintain a project file (xml type syntax, use visual studio to create a project and then open that in notepad for an example) but i don't have any experience with it personally.
It's possible to do it all from notepad and the command prompt. You will probably find it to be a little more time consuming however.
If you want to learn c# and winforms, part of the skill set you need is being proficient in Visual Studio.
You can do things in Notepad for awhile (I wrote some web services in notepad once because I didn't have VS available), but eventually you will want to take advantage of the tools available in VS.
I highly recommend you use Visual Studio (Microsoft offers free Express versions that will meet your needs). Learning the syntax of the language is wonderful, but you must be able to work within the VS environment to be truly successful in C# (and any of the .NET languages). It benefits you more to do it right and learn it all together rather than try and piece it together later. Just my own two cents.
Just to repeat what's already said, again with no disrespect, you are not going to learn .NET in notepad. It's just not probable. Not only are you not be productive, but you're also not going to learn the tools used in the industry, best practices, and other important factors about .NET. It's not just about sitting down and writing code. By limiting yourself to notepad, it's like limiting yourself to one meal a month: you lack the nutrition needed to keep moving forward at a good pace.
Utilize the tools and resources available to you. Limiting yourself like that is a kick in the rear end.
Use sharpdevelop (Windows) or monodevelop (*nix). Both have Windows.Form support. Although they dont offer as much as VS, they'll at least get you started. I've never used the VS Express edition, so I don't know what it's limitations are.
Note that notepad is not even the equal of vi not to speak of vim.
If you want to use a text editor then you could try it but I don't see the point of using notepad. Use a real text editor, not necessarily vim/emacs, you could pick a nice gui text editor like notepad++ or kate.
Notepad can't even display unix line endings(I think).
Go download an Express edition of Visual Studio. I understand the possibility of thoroughly learning this via notepad, but with a free IDE out there, it makes no sense.
if u have something against MS or VS.net u can try sharpdevelop
http://www.icsharpcode.net/OpenSource/SD/
last time i looked it was xcopy deployable :)
I don't know where to find tutorials, but the approach is pretty straightforward: import System.Windows.Forms, derive a class from Form, and give it a .Show(). Your components are members of the class, and can be positioned using their respective position/size properties. Events are handled through delegates.
I do see where this technique is useful, though I'd use a decent IDE instead of Notepad. .NET is just too prolific. Knowing how to construct forms on the fly can come in handy.
If you're looking for an alternate IDE, check out icSharpCode's SharpDevelop.
Also, look into JScript.NET tutorials - there is no forms editor for that language, as inheritance simply isn't possible. But it's still based on .NET, so the basic motions are the same. Here's a decent one:
http://www.webreference.com/js/column117/
I know this is answered by strangely I haven't seen anyone talk about NAnt.
For years I developed in TextPad, with some syntax highlighting, + NAnt to do my builds. These days it'd be even easier as you can have your buildserver prep a proper msbuild for you (CC.NET + NAnt).
I also learned about a few things that physically couldn't do in Visual Studio (at the time it was .net 1.1). Definitely a good experience, and I'd recommend it really. Probably not for winforms though, because the designer is actually useful for that.
i am a notepad user. :) i don't have visual studio installed on my computer.
this is what im doing.
1st u must register your .net framework folder on Environmen Variables.. Path
or run on CMD this lines
path=%path%;(this is where ur .net framework address were)
(ex path=%path%;C:\Windows\Microsoft.NET\Framework\v4.0.30319)
then hit enter
2nd to compile a single notepad(save as .cs), locate the destination of the file using cmd. then. type "csc nameOfCS.cs"
for multi file.. "csc ManinCsForname.cs classes.cs classes.cs"
it will compile as exe.
for more command.
"csc /?"
its ok to use Visual Studio. but if you want to become more familiar with C# and structure. or can make a system on any PC. without using any IDE. u can do this.
Not using Notepad will help. Crimson Editor or TextPad or others have line numbering, syntax highlighting and many features you'd need. I'm sure Notepad has file size limitations which you might run into.
The sans-IDE element I can only fully answer from a Java point of view. I've done a fair amount of Java UI development using Crimson Editor, the Java SDK, batch files and/or either ANT or Maven at times. If you developing UI code that's generic or does a fair amount in dynamically then its ok. If your work involves designing many different specific Forms (i.e. screens that have many customer forms and aren't subject too much reuse) then the Designer features of the IDE are extremely useful.
However, I have found .NET IDE development a little frustrating coming from the above model of Java development.