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.
Related
I love Clojure. It is by far my favorite language.
I've just found Xamarin, which allows you to have cross-platform apps, making full use of native libraries. The language used with it is C#.
Now I'm wondering if there is any way to program the majority of my Xamarin apps in Clojure (CLR), instead of C#.
So, Clojure CLR will let you use any libraries it can load. For example, I've loaded the Oracle.DataAccess libraries, and the NLog libraries into Clojure CLR, so it's theoretically possible to load the Xamarin libraries the same way:
;;; not sure which library you want to include, using this as a placeholder
(assembly-load-from "C:/Path/To/Xamarin.Core.dll")
That being said, Xamarin itself appears to also be an IDE, and a set of build tools on top of their own proprietary libraries and build tools, so it may not be possible to fully integrate the two. Also, I think Xamarin works purely with C#, so coding in Clojure is probably not possible...directly.
What you can do is create some libraries in Clojure CLR, and compile them down to DLLs, which you can then link to, so you can build the bulk of your logic in Clojure, and then create some simple C# wrappers that your Xamarin app consume.
I'd love to hear back on what you try, and whether or not you're successful doing this.
Best of luck!
Possibly yes in Android, but in the case of iOS I would say it's not likely, because the latter is a platform which has a lot of limitations around generated code, reflection, and the like. And Clojure being dynamically typed has high chances of being affected.
Honestly, the Clojure in the .NET world would fit more with F# (also functional, immutable by default, etc), which is a language that has just been adopted as official by Xamarin (meaning you can use it in Android and iOS, and Mac).
It's also statically typed (as opposed to Clojure) so this may mean that it's also faster. Give it a try!
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
I'm looking for a embedded language that can be used to script common code between a server app written in C# and a client app written in Flash/Actionscript. It is important that the embedded language interpreter run natively in the environment it is embedded into. For example, it cannot load any external C libraries (i.e. python, neko).
Does such a thing exist for these two languages? Perhaps there is some minimal interpreter that could be easily ported to both?
Thanks!
I believe Javascript is the answer.
Here's some thoughts on calling Javascript from Actionscript and vice versa.
Here's some words about calling Javascript from C#.
It would probably end up fairly ugly, but someone has successfully embedded Lua into Actionscript through Alchemy -- demo on Google Code. Pair that up with LuaInterface and you've got an embedded language on both sides. You'll find a couple more "JavaScript (or ActionScript in Flash)" implementations of Lua on the Lua Implementations page.
There's also Pair, an AS3 implementation of Python that looks a lot more stable than Lua-Alchemy. The .NET complement would be IronPython.
Haxe can output a .swf, which might suffice for your needs -- I'm not sure if two .swf files can communicate, I don't really use flash. The big downside with Haxe is no native .NET support, although it can support Neko or C++, both of which you would have to go out of your way to integrate with .NET.
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.
I currently use Python for most of my programming projects (mainly rapid development of small programs and prototypes). I'd like to invest time in learning a language that gives me the flexibility to use various Microsoft tools and APIs whenever the opportunity arises. I'm trying to decide between IronPython and C#. Since Python is my favorite programming language (mainly because of its conciseness and clean syntax), IronPython sounds like the ideal option. Yet after reading about it a little bit I have several questions.
For those of you who have used IronPython, does it ever become unclear where classic Python ends and .NET begins? For example, there appears to be significant overlap in functionality between the .NET libraries and the Python standard library, so when I need to do string operations or parse XML, I'm unclear which library I'm supposed to use. Also, I'm unclear when I'm supposed to use Python versus .NET data types in my code. For example, which of the following would I be using in my code?
d = {}
or
d = System.Collections.Hashtable()
(By the way, it seems that if I do a lot of things like the latter I might lose some of the conciseness, which is why I favor Python in the first place.)
Another issue is that a number of Microsoft's developer tools, such as .NET CF and Xbox XNA, are not available in IronPython. Are there more situations where IronPython wouldn't give me the full reach of C#?
I've built a large-scale application in IronPython bound with C#.
It's almost completely seamless. The only things missing in IronPython from the true "python" feel are the C-based libraries (gotta use .NET for those) and IDLE.
The language interacts with other .NET languages like a dream... Specifically if you embed the interpreter and bind variables by reference.
By the way, a hash in IronPython is declared:
d = {}
Just be aware that it's actually an IronPython.Dict object, and not a C# dictionary. That said, the conversions often work invisibly if you pass it to a .NET class, and if you need to convert explicitly, there are built-ins that do it just fine.
All in all, an awesome language to use with .NET, if you have reason to.
Just a word of advice: Avoid the Visual Studio IronPython IDE like the plague. I found the automatic line completions screwed up on indentation, between spaces and tabs. Now -that- is a difficult-to-trace bug inserted into code.
I'd suggest taking a look at Boo [http://boo.codehaus.org/], a .NET-based language with a syntax inspired by Python, but which provides the full range of .NET 3.5 functionality.
IronPython is great for using .NET-centric libraries -- but it isn't well-suited to creating them due to underlying differences in how the languages do typing. As Boo does inference-based typing at compile time except where duck typing is explicitly requested (or a specific type is given by the user), it lets you build .NET-centric libraries easily usable from C# (and other languages') code, which IronPython isn't suitable for; also, as it has to do less introspection at runtime, Boo compiles to faster code.