the book I'm reading went briefly over command line building, specifically controlling the linker.
But any class and .cs file containing classes or resources seems to link just fine if they reside inside my project, can I just ignore the command line builder for now until I'm more profficient in C# or is this something I need to know right now?
CommandLine builder using csc.exe in my knowledge is not used by even experienced professionals. Everyone goes the route of Visual Studio latest versions and it is safe to ignore commandline building.
But you can try and understand how it works. Because, in the end this is the one which is used by your GUI tools like Visual Studio to do the build.
I would generally say it is worth knowing how to use the command line building, but it is not a necessity. I started off just using Visual Studio and wasn't even aware of command line building for C#. However, since I learned to use it and write Command Script files to execute it, I actually use it fairly often - especially when working on large projects that have multiple dependencies in need of building. In that situation, it saves opening multiple Visual Studio instances.
Agreed with Muthu. Building via CSC.exe is good option and works for simple setups or for learning, but if you have a complex solution structure in Visual Studio using inter-project dependencies, post build events, etc. its best to just invoke the Visual Studio build call itself via the command line. The command is devenv.exe and you'll find lots of documentation on it.
If you are trying to learn C# complier/linker innards maybe you'll be more interested in Project Roslyn from Microsoft as they try to open the "black box" of the compiler.
Related
So what I need is some why to write a solution analyzer for Visual Studio, that can:
detect move class and move method refactorings.
display the recommended refactorings in Visual Studio.
apply the refactorings.
I have a console application that can do this (using the Roslyn compiler), but I want to integrate the logic inside Visual Studio. From what I have read, the current code fix/refactoring/analyzers support only document level of refactoring, but for me that is not enough information to recommend one of the mentioned refactorings. I need information about the whole solution.
So my question is what is the best way to do this? Can you recommend a starting point? Some articles related to the topic? Any advice would help me a lot.
Short Answer: There is no reasonable way to do this with the Roslyn API
Long Answer:
The api as it is currently implemented only allows analyzers to know about things in the current compilation (a project in Visual Studio). If you call RegisterCompilationAction from within the Initalize method in your analyzer, you will be able to look at all the symbols within the compilation.
Why can't analyzers see the scope for an entire solution? The simplest answer is: because the compiler can't, and analyzers run inside the compiler. This is done so that analyzers can be run on continuous integration servers without Visual Studio installed. MSBuild reads the solution file and then invokes the compiler once for each project. The compiler is never aware of project dependencies and the compiler team does not want to be in that business, they are happy to leave it to MSBuild.
People have tried to work around this by loading their solution using MSBuildWorkspace and attempting to look at documents across projects that way. This will fail occasionally because MSBuildWorkspace is not thread-safe. It will also cause memory usage to skyrocket. People have tried to cache MSBuildWorkspace instances to partially resolve this problem but the cache needs to be invalidated every time a new compilation is created (essentially in the event of all but the most trivial changes). Basically, going down this path is rife with pain and is unsupported.
Enough people have asked for this feature that its something we think we need to do eventually. There is no reasonable way to accomplish it today unless you are willing to write a Visual Studio extension that imports the Visual Studio Workspace and attempts to run its own analysis engine. Please file feature request on https://github.com/dotnet/roslyn
I have a few questions:
Is there any way through which I could create a Visual Studio Project, add files to it, and build it from within a Python program?
Are there built-in commands to do this? If not any commands which could be run in command line?
Thanks for the help.
is there any way through which I could create a Visual Studio Project, add files to it
Whilst there is a .NET API for creating/manipulating project files, it's a bit on the undocumented side (I have used it in the past though) and I don't know if you can call it from Python. If you want to see the .NET API just look at the IronPython Custom Project Extension project.
However, VS project files are just XML files so if you know the schema, you can just write to the files from Python using your API of choice. VS won't know any better.
and build it from within the python program
Ultimately you can just spawn a process to invoke msbuild. Works for Jenkins.
Recently it looks like Microsoft has released Python tools for Visual Studio found here.
There is an Iron Python NuGet package that is tightly integrated with .NET, but not sure you'd want to use this for pure Python programming. I would also recommend Jetbrain's PyCharm which is an IDE also suited for Python or just use Notepad++ (free) and compile from the command line.
I work in a game engine called Unity3D whose scripting system runs on Mono. By default Unity uses a modified version of Mono to develop/compile script. I am in the process of setting up my Visual Studio to better work with Unity projects and its various gotchas with regards to .NET.
I am currently stuck trying to configure Visual Studio to handle compilation of my various assemblies. Unity uses a modified version of an old Mono compiler, and as such I cannot use the default csc.exe to build assemblies.
My ideal solution would be for Visual Studios's Build Solution option to process my projects with a compiler I point it to, perhaps through configuring the MSBuild (.csproj) file? Falling short of this is there another way I can build from within Visual Studio using Mono's mcs/gmcs compiler?
One approach would be to write an MSBuild/PSake script that you could run via a shortcut key from within VS. I've written a blog post about hooking a build script up via a shortcut key. Basically, you would write a batch file that executes the MSBuild script and assign a keyboard shortcut to it, say ALT-1. You would then hit ALT-1 instead of CTRL-SHIFT-B to compile the project.
I did a project where Unity was running on my Mac and ran Windows/VS2008 on Parallels. In my Visual Studio solution I referenced the appropriate Mono dlls instead of the normal .NET dlls (i.e. System, System.Xml, etc.) and used the typical .. I also wrote all my unit tests using the nUnit dlls provided with the Mono distribution. Since Unity is (or at least was) using just the .cs files, simply copying (via Post Build Script) them into my Unity project structure worked great. Unity would detect the change and update the scripts. This method was very effective, however I was using it about 2 years ago so I'm not sure if it still applies.
In addition it appears that someone went through the trouble of making a video on how to do this:
http://forum.unity3d.com/threads/120327-Video-Tutorial-How-to-use-Visual-Studio-for-all-your-Unity-development
I use Visual Studio 2010 for C# development. But, there are many times during the day when I need to quickly try out a small bit of C# code to experiment or learn something new. I can write this as a fully functional C# program in a single file. Now, I can use Visual Studio for writing/editing this C# files, but it does not support execution or debugging (breakpoint, watch variables) unless I create a solution for it.
I would like to have the ability to create, edit, execute (and hopefully debug) single C# files, without having to go through the pains of creating a VS solution everytime I need to try something. Is there a simple C# IDE on Windows in which I can do this?
The IDE should not create any project/solution files. That way I can have a directory of C# files, each of which is a full C# program and each of which I can open to read, edit and execute with this IDE anytime I want.
LinqPad can execute expressions, statements, or small programs that could be a single file in size (also see this page). It does not create projects/solution files just as you are seeking. Just open, code, and save if you like. I use the free version to test code I post on this site.
It supports .Net 4.0 and has some other nice features, such as not needing to explicitly specify using statements. The program is smart enough to know which ones you need.
Try Snippet Compiler
In the next release of C# and Visual Studio, one of the new features is Project Roslyn, a compiler-as-a-service implementation. The upshot is that this enables is scripting in C#, with .csx files. The Visual Studio blog has a lot more information on .csx files here.
I myself haven't played with .csx files much, but they seem similar to .fsx files for scripting F#, in that you take the code that you would normally place inside a method (in F#, a function) and place it at top-level, and can use the hash to introduce compiler directives (e.g. #r to reference a DLL).
(This question is rather similar, but the only answer does not seem to be answering my needs).
I am thinking of using Thrift in C#, and am considering how exactly the build process would work. Do C# projects in Visual Studio 2008 support custom build actions that generate C# classes?
I found the "Custom Tool" option, but I'm not sure it's what I'm looking for ... it only allows design-time usage (not integral to the build process, but rather right-click "Run Custom Tool").
Update
Prebuild events that Fionn suggested are indeed suboptimal, as they don't take dependencies into account and prolong the build process. Also, they are managed from a central location instead of per-file.
What if you simply put your code generation to the pre-build events.
They are found in the "Build Events" section of the Project Properties in Visual Studio.
Also they support some macros which you will see if you use "Edit Pre-build ..." button.
The simplest and safest option is to include the generation in the "Pre Build Events" as Fionn mentions. Pros: code is always correct, Cons: Slow as this will mean at each compile that you re-generate the source code, and then rebuild all dependencies, as the generated code as altered.
Another option is to manually regenerate the code files, and if you have a build machine/continuous integration rebuild the code files each build. Pros: Faster builds, if there are few changes Cons: out of date generated code, and hours of wasted debugging trying to solve what's wrong.
Reading the C# project help, it seems that you need a plugin that provides the Custom Tool action you want. But in C++ projects you can define your own custom step MS-Help. So you could develop a VisualStudio plugin (or find one) or add a C++ project to your solution, and add a custom build step to that project, and then include the output in your normal C# project.
The first google for 'visual studio custom tool code gen' has a how-to write your own Custom Tool which would be a good starting point.
MS Build is highly configurable. Visual Studio itself is also very extensible. You can use the CodeDOM to emit classes. Depending on how much time you want to put into customizing VS and the build process, there is very little you cannot do.