So in Python on a unix environment, for example, we can open up python from terminal, start writing code and immediately run it to test some python library functions. Is there a similar commandline/scripting environment for C# or Java (perhaps a plugin to the IDE?)? Such a tool would seem pretty useful for experimenting with something like the System library.
Relatedly, I read that some versions of Python are compiled into bytecode before being executed by a virtual environment, which sounds like Java and C#. But, the Python program I open up in terminal and start typing code into has to be interpreted, right? So based on principle, it seems like it is possible to write a C# or Java interpreter. So I'm not sure why I just haven't seen anyone interacting with C# or Java on the commandline the way they do with Python.
Thanks.
For .NET there is LINQPad. It supports C#, VB.NET and F#.
The name suggests that it is only for LINQ but you can execute any code in there. There is no need for a database.
For .NET, if LINQPad is overkill for you, try the RunNET command-line package. It requires that you load script files instead of interactive typing, but lets you leverage more languages.
The ability to explore .NET types was one of the reasons I originally started learning F#, since it has an Interactive environment (in Visual Studio, click on View > Other Windows > F# Interactive.) Upon digging deeper into F# I have found it to be one of my favorite programming languages.
Anyways, you may want to look at it. It won't take long to figure out the basic syntax, and once you've got it you can explore various .NET objects in the Interactive Environment, if you don't want to wait for Roslyn.
For a tutorial, check out this page: http://www.tryfsharp.org/Tutorials.aspx
Related
I am looking to replace an old control being used for scripting an application. The control used to be called SAX Basic, but is now called WinWrap. It provides us with two primary functions.
It's a scripting engine (VB)
It has a GUI for developing and debugging scripts that get run in the hosting application.
The first feature it provides is actually pretty easy to replace. There are so many great methods of running just about any kind of code at runtime that it's almost a non-issue. Just about any language targeting the .NET runtime will work for us. We've looked at running C#, PowerShell, VB.NET, IronPython, etc. I've also taken a brief look at Lua and F#, but honestly the language isn't the biggest barrier here.
Now, for the hard part that seems to keep getting me stuck. We want a code editor, and debugger. Something simple, not unlike PowerShell's ISE would be fine. Just as long as a file could be created, saved, debugged and executed.
I'm currently looking into Visual Studio 2010 Shell (Isolated) and I'm also looking at the feasibility of embedding PowerShell ISE in my application. Are there any other editors I could embed/use in my application? Purchasing a product is not out of the question. It comes down to a combination of ease of use, how well it meets our needs, and how simple deployment and licensing is for developers.
While I have not used this solution myself, I would recommend that you look into SharpDevelop (OSS .NET IDE) - it has text editor controls that have been embedded in other applications, for instance Kaxaml.
I have a need to create a utility in Suze Linux. The utility will make modifications to some text files, and then use the information in those text files to program a device in the computer using a different executable which accepts command line parameters.
I am fluent in c#, but have never worked with Linux. Should I take the time to learn Gnu C++ to do this, or install Mono? How would I execute the programming utility and pass it command line parameters?
Is there a reason you want to restrict yourself to only C++ or C#? There are many options you could consider, for example:
For very simple tasks:
Bash: In some cases a simple Bash script will be able to solve the task. Piping information from one process to another is a breeze, and you have the power of sed, awk, etc. at your fingertips. Another major advantage is that it is installed almost everywhere.
For slightly more involved tasks you could try a scripting language:
Python: Easy to learn, pleasant to read. Very useful for putting together small applications quickly. You can use subprocess to communicate with other processes.
Ruby: Similar comments to Python - a good scripting language with a clean syntax.
Perl: Perl is very good at processing text, although personally I dislike the syntax.
Other options:
Java: Your C# experience will allow you to learn Java quickly as it is a very similar language. Java is officially supported on Linux.
C#: A lot of Linux users are wary of C# because it isn't free enough, but obviously that isn't a worry for you. It has worked fine the few times I've tried it. Note that Mono is not 100% compatible with Microsoft's version.
C++: For what you're planning to do I personally wouldn't recommend C++. It will solve the task though, and if you already know some C++ then I guess it is worth considering.
Obviously there are many other suitable options too.
In C and C++ there's system("command arguments") which you can use to execute a command using the system's interpreter (i.e. you can use it as though you had typed in the command in the shell). The command string can be constructed at runtime. I'm not very familiar with C#, but if I recall correctly you can use Process and ProcessStartInfo classes to run system commands.
Based on the complexity of your program I'd recommend using a scripting language like Perl. It's always a good idea to have a scripting language in your toolbox.
I want to enhance an application with scripting support like many other applications have, e.g. MS Office using VBA or UltraEdit using JavaScript.
Which libraries do exist for C#/.NET (and which language(s) do they support)?
Please check CS Scripting library
Here is an article about scripting Photoshop CS with C#
This one discusses using LUA as scripting lib with C#.
IronPython is a dynamic .NET scripting language.
IronPython is an implementation of the Python programming language running under .NET and Silverlight. It supports an interactive console with fully dynamic compilation. It's well integrated with the rest of the .NET Framework and makes all .NET libraries easily available to Python programmers, while maintaining compatibility with the Python language.
See IronPython embedding for examples showing ways to call IronPython from .NET apps.
The IronPython Calculator and the Evaluator goes into the details of using IronPython from a C# application.
Lua is often touted as being one of the better ones... Try looking at this other question for more information: What are the most effective ways to use Lua with C#?
Also:
Lua Interface
Binding code to Lua
See "What is the best scripting language to embed in a C# desktop application."
Also see "It Already Is A Scripting Language" from Eric Lippert.
Don't forget LSharp, LISP in .NET. Something to keep an eye on if you are exploring functionality stage. Maybe Rob Blackwell will be glad to hear you're considering it.
You can bake your own scripting environment with Mono.CSharp (just one simple dll) or Roslyn, both are getting quite mature now.
Mono contains the Evaluator class and Roslyn the ScriptEngine, both make it a breeze setting up a script environment. Of course something like ScriptCS already builds on that (Roslyn) and gives you more features.
For an C# script environment built on Mono.CSharp you can check out CShell (which I made).
Depending on your needs, the SILK library might be a good option.
It's an easy to use interpreter. The interpreted language is not C#. It's a custom language that was designed to be easy to use (very little punctuation, not case sensitive, etc.) But it does support functions.
Built-in functions are handled via events. That is, when the interpreted code calls one of your internal functions, it raises an event in your program.
I would like to write simple scripts in C#. Stuff I would normally use .bat or 4NT .btm files for. Copying files, parsing text, asking user input, and so on. Fairly simple but doing this stuff right in a batch file is really hard (no exceptions for example).
I'm familiar with command line "scripting" wrappers like AxScript so that gets me part of the way there. What I'm missing is the easy file-manipulation framework. I want to be able to do cd(".."), copy(srcFile, destFile) type functionality.
Tools I have tried:
NANT, which we use in our build process. Not a good scripting tool. Insanely verbose XML syntax and to add a simple function you must write an extension assembly. Can't do it inline.
PowerShell. Looks great, but I just haven't been able to switch over to this as my primary shell. Too many differences from 4NT. Whatever I do needs to run from an ordinary command prompt and not require a special shell to run it through. Can PowerShell be used as a script executor?
Perl/Python/Ruby. Really hate learning an entirely new language and framework just to do batch file operations. Haven't been able to dedicate the time I need to do this. Plus, we're a 99% .NET shop for our toolchain and I really want to leverage our existing experience and codebase.
Are there frameworks out there that are trying to solve this problem of "make a batch file in C#" that you have used?
I want the power of C#/.NET with the immediate-mode type functionality of a typical cmd.exe shell language. Am I alone in wanting something like this?
I would try to get over the PowerShell anxiety because it is the shell of the future. All of the software coming out of Microsoft is using it as their management interface and especially version 2.0 is ridiculously useful.
I'm a C# developer most of the time but PowerShell has solved that whole "WindowsApplication42" problem of temp projects just piling up. PowerShell gives you full access to the .NET framework in a command line shell so even if you don't know how to do something in PowerShell, you most likely know how to do it in .NET.
IronPython and IronRuby do let you leverage all of your .NET "experience and codebase" (they don't answer your objection to learning new languages, however).
If you have any bash nerds, you can always try cygwin.
Also remember that Python was originally intended as a "glue" langauge. If you used the aforementioned IronPython, it's pretty easy to tie together pre-written C# classes.
If you are bind to MS, PowerShell is surely the way to go. But I don't like it much.
I personally use MSBuild script more, and would like to see Mono C# Shell one day comes to Windows.
I think CS-Script might be the ideal solution for you.
I was learning python using the tutorial that comes with the standard python installation. One of the benefits that the author states about python is "maybe you’ve written a program that could use an extension language, and you don’t want to design and implement a whole new language for your application" - My question is how would i go about designing a program (using c#) that can be extended using Python interactively(for this to be possible, i would imagine that i would need to create some sort of a "shell" or "interactive" mode for the .net program) ?
Are there any pointers on how to design .NET programs that have an interactive shell. I would then like to use python script in the shell to "extend" or interact with the program.
EDIT: This question partly stems from the demo give by Miguel de Icaza during PDC 2008 where he showed the interactive csharp command prompt, C# 4.0 i think also has this "compiler as a service" feature. I looked at that and thought how cool would it be to design a windows or web program in .NET that had a interactive shell.. and a scripting language like python could be used to extend the features provided by the program.
Also, i started thinking about this kind of functionality after reading one of Steve Yegge's essays where he talks about systems that live forever.
This sounds like a great use of IronPython.
It's fairly easy to set up a simple scripting host from C# to allow calls into IronPython scripts, as well as allowing IronPython to call into your C# code. There are samples and examples on the CodePlex site that show how to do this very thing.
Another good site for examples and samples is ironpython.info
And here is a page dedicated to an example answering your very question, albeit in a generic DLR-centric way -- this would allow you to host IronPython, IronRuby, or whatever DLR languages you want to support.
I've used these examples in the past to create an IronPython environment inside a private installation of ScrewTurn Wiki - it allowed me to create very expressive Wiki templates and proved to be very useful in general.
I was looking solution for the same problem, and found IronTextBox: http://www.codeproject.com/KB/edit/irontextbox2.aspx
It needs a little tuning for current versions, but seems to be everything I needed. First made it compile, and then added variables I wanted to access from shell to the scope.
Python as an extension language is called "Embedding Python".
you can call a python module from c++ by bascially calling the python intepreter and have it execute the python source. This is called embedding.
It works from C and C++, and will probably work just as well from C#.
And no, you do not need any kind of "shell". While Python can be interactive, that's not a requirement at all.
Here is a link to a blog post about adding IronRuby to script a C# application.
http://blog.jimmy.schementi.com/2008/11/adding-scripting-to-c-silverlight-app.html
The principles would also work well for using IronPython.
If your goal is to avoid learning a new language you can use CSScript.Net and embedded scripts written in C# or VB into you application. With CSScript you get full access to the CLR. Three different models of script execution are supported so that you can execute script that refers to objects in your current app domain, execute using remoting, or execute as a shell.
Currently I am using CCScript as "glue" code for configuring application objects somewhat similar to using Boo.
This link tasks you to a code project article that provides a good overview.
I don't know what you mean with
"extend" or interact with the program
so I can't answer your question. Can you give an example?
There is an open source interactive C# shell in mono: http://www.mono-project.com/CsharpRepl
When you like python, .Net and language extension, you will probably like Boo over Iron python. Boo comes with an open source interactive shell too.
I disagree with
"you don’t want to design and
implement a whole new language for
your application"
It's not as hard as it used to be to create a simple DSL. It won't take you days to implement, just hours. It might be an interesting option.