I’m trying to make CodeContracts in C# look more like statements so i need a tool that allows me substitute some code before the compiler sees it, if there is no way to do it i guess a Visual Studio plug in would help too.
Trying to fight the syntax of a language like C# is simply a bad idea. The tools you use will fight back with determination.
Just go with the flow and stop trying to swim against the current. You'll soon get used to the syntax and it will make sense to everyone else who works with the code.
You could always channel the code through a C++ preprocessor (like mcpp) before sending the code to the C# compiler. That would give you full power of C++ macros :)
I guess you could use a pre-build task to do that.
I use this approach in one project of mine (though it is not C#, but a quite different language, but I needed the C++ like macros).
What is wrong with the normal codecontracts ?
http://www.cauldwell.net/patrick/blog/CodeContracts.aspx
And
http://weblogs.asp.net/podwysocki/archive/2008/11/08/code-contracts-for-net-4-0-spec-comes-alive.aspx
What about expression trees?
Related
I know it might not be worth it but just for education purposes I want to know if there is a way to inject your own keywords to .NET languages.
For example I thought it's good to have C++ asm keyword in C#.
Remember I'm not talking about how to implement asm keyword but a general way to add keyword to C#.
My imagined code :
asm{
mov ax,1
add ax,4
}
So is there a way to achieve this ?
The answers which cover implementing keyword{ } suits enough for this question.
This isn't possible at the moment. However, there's a Microsoft project in development called Roslyn that can be summarised as "the compiler as a service." It allows you, amongst other things, to extend or modify the behaviour of the compiler through an API.
When Roslyn becomes available, I believe this should be something that (with caution!) is quite doable.
You can use whatever tools you would like to pre-process your code before sending it to the C# compiler. For example, you might use VS macros to do the pre-processing, mapping a given syntax that you invented into something that does compile into C# code, possibly generating an error if there is a problem. If VS macros aren't powerful enough for you then you can always use your own IDE that does whatever you code it to do to the text before sending it to the compiler.
There is no built in support in the compiler for specifying your own keywords/syntax; you would need to handle it entirely independent of the compiler.
Unfortunately this is not possible. You can't extend or alter the languages in any way.
You could in some obscure way use PostSharp to read and parse strings and transform them to custom code at compile time (a pre processor). But you would not get very happy with that, as it is very error prone and you won't get any kind of intellisense or code completion for your magic strings.
According to MSDN keywords are predefined and cannot be altered. So you can't add any, because you would need to tell the compiler how to handle them. Insofar, no you can't.
For my university course I want to make system for learning programming. The main idea is writing code through debug, just look at picture (from Bret Victor video):
I don't want to make my own compiler (just becouse it's very complicated and hard, especially for C# language), so I want use all features of .NET and other libraries. I see there two ways:
Rewrite code by replacing assigments to assigment & sending debug information to main program
Compile code and debug it -> ...
Anyway, I need some start point. What classes and libraries I must google, in which manuals I can read useful information, what is the best way to implement it?
I think what could work is, if you just treat C# as a script language. There are multiple tutorials on how to use C# within C# as a script language, like this one. So you just start the "script" compiler again on predefined conditions (like a line break) and if the compilation is successfull you return the result in your second pane. This is just a basic idea.
I need to run small snippets of C# code for educational purposes and for each execution, I should open the project (solution), delete existing code, type new code, build and compile, and then run the project. For example, for executing string.IsNullOrEmpty("something") I should follow all this procedure.
I just thought of something like:
In which I can enter code snippets, click execute, and then see the result. Is there anyway to do that?
You didn't explicitly state if you're looking for guidance on implementing your own solution or what, but if you're open to using a third party utility then LINQPad is pretty much exactly what you're describing.
Take a look at Snippet Compiler.
Not exactly what you're asking for, but from what I can tell it seems pretty close.
There is also ideone which can run C# code, as well as a large number of other languages.
There is also mono's csharp/gsharp:
see http://www.mono-project.com/CsharpRepl
Mono are doing the compiler as service stuff - this is what you are looking for: http://tirania.org/blog/archive/2010/Apr-27.html
I require the ability to preprocess a number of C# files as a prebuild step for a project, detect the start of methods, and insert generated code at the start of the method, before any existing code. I am, however, having a problem detecting the opening of a method. I initially tried a regular expression to match, but ended up with far too many false positives.
I would use reflection, but the MethodInfo class does not reference the point in the original source.
EDIT: What I am really trying to do here is to support pre-conditions on methods, that pre-condition code being determined by attributes on the method. My initial thought being that I could look for the beginning of the method, and then insert generated code for handling the pre-conditions.
Is there a better way to do this? I am open to creating a Visual Studio Addin if need be.
This is a .NET 2.0 project.
Cheers
PostSharp or Mono.Cecil will let you do this cleanly by altering the generated code without getting into writing a C# parser which is unlikely to be core business for you...
Havent done anything of consequence with PostSharp but would be guessing its more appropriate than Mono for implementing something like preconditions or AOP. Alternately you might be able to do something AOPy with a DI container like Ninject
But of course the applicability of this idea Depends - you didnt say much other than that you wanted to insert code at the start of methods...
EDIT: In light of your desire to do preconditions... Code Contracts in .net 4 is definitely in that direction.
What sort of a tool do you have? Whats wrong with having a single Mono.Cecil.dll DLL shipped? Either way something other than a parser is the tool for the job.
I am sure there is an easier way but this might be a good excuse to take MGrammer for a spin.
Any suggestions on how I should approach this? Thanks.
Take a look at this VB to C# Comparison chart for some of the syntax and keyword differences.
I have to do this often - and my biggest hang-up is the semi-colon. Never fails that my first few days of writing VB after a longer stint of C# coding, the VB compiler is always barking at me for putting a semi-colon on every line of VB code.
Other than that, it shouldn't be too painful. If you're fluent in C#, moving to VB might be stressful for the first few days, but after that you should be smooth sailing.
Code converter tools come in handy to help you remember/learn/re-learn all of those odd syntax differences that you forget easily. The one I normally turn to first is http://converter.telerik.com/ - and if that won't do the trick, a quick google search for code converters will turn up a handful of other good ones.
Another pain point that I've had in the past too is Snippets. Snippets in C# rock - but in VB rock a bit less. Get to know the differences between those and life will be much easier. (Come on VB team - get that enter key working like the C# snippet team has it...)
A good C# to VB.NET converter will help.
Aside from revulsion and horror I recommend (from experience - ugh) to just start. Build a simple app. The magic is in the experience. It doesn't make sense until you have spent lots of time trying to figure out why something doesn't work.
I went the other way (VB to C#) and found the syntax to be so similar that the transition was painless. I can now pretty much program in either platform – thanks a lot to the IDE intellisense.
Take advantage of the "With" statement! One of my favorite parts of VB.NET.
It's not as difficult as it seems at first. Took me about a month from going strictly C++\C# to VB to get comfortable.
If you are familiar with programing you should just have to learn the syntax...why would anyone want to go from C# to VB? who knows :)
My first question would be 'Why?'. I'd like to think that you can pretty much get the same thing done with either C# or VB.Net. Given that it's managed code, why not just leave them as they are?
Let's just assume you have your reasons :)
1) There are a couple of tools that will do this (see http://www.developerfusion.com/tools/convert/csharp-to-vb/ for a sample).
2) The other option is to manually convert the code, compile, fix errors, and repeat. Painful.
It's a pretty straight-forward thing, actually. VB.Net is a perfectly good (if, imo, verbose) language with most of the expressiveness you've grown accustomed to in C#. Just be aware that certain specific keywords are different and that you've got a different background culture and you'll do fine.
You can also use a tool like CodeRush from DevExpress (no affiliation). The short-cut keys for any operation are the same for both languages and will produce the correct output for the language.
For example: key combo "mv" yields:
In C#
public void MethodName ()
{
}
In VB
Public Sub MethodName()
End Sub
Use XML literals and marvel how resentful fellow C# programmers suddenly are.
There were some useful articles in Visual Studio magazine back in Jan 2008.
What C# developers should know about VB
And for completeness: what VB developers should know about C#