I'd like some help in how to add scripting support to a WPF C# project I'm doing on Visual Studio 2015. One of the things I'd like to do is to be able to change User Control properties within that script. I've being trying Roslyn C#, and I read some stuff about IronPython and PowerShell Tools. But, all that information is not really helping.
So, do you have a simple answer? Like, the easiest way to execute scripts in Visual Studio 2015 C# WPF Application, that are able to change properties of User Controls within the project?
Any help would be appreciated.
Thanks,
Lucas.
T4 templates can help but they are less extensible than lets say Roslyn.
I found a great post on how to easily implement IronPython and how to access User Controls within the scripts in Python, so you can there change different Control properties, and even create them! Here's the link:http://www.codeproject.com/Articles/602112/Scripting-NET-Applications-with-IronPython
I think it should be quite simple to embed powershell or F# interactive in an application and there are lots of examples when we search the web for this. The question is only are we comfortable writing scripts in Powershell and F#?
In my application I provide a C# editor and compile scripts as static methods (which I uses as functions) using the standard .Net compiler API's which are installed with the framework (Microsoft.Csharp, System.CodeDom). This generates a temporary dll and perhaps can not be properly termed a script. The implementation is straight forward and more or less what you expect - I have to provide a way for the users to specify references and namespaces, then generate a source file, compile it to an assembly and then call members of the static class. This above technique works well because the application has extension points that are well adapted to user defined code with a certain well defined structure (you might have similar well defined needs and perhaps full scripting is not necessary).
On the other hand, I am considering providing a "super user" console where the application can be driven by script, and in this case, I will use either Powershell or F-sharp interactive (I'm leaning towards F#). I don't think the implementation is particularly complex, but these are things that can easily bring the application down or corrupt application data if it is not well controlled - and it is unlikely that my users will profit from scripting as it is a programmer environment.
Related
I am currently working on creating a GUI for an interface that will be used in several languages.
The environment I am working in is Unity 4.2 with Mono C#. The application must remain very cross platform compatible (including web, ios, Linux, windows).
What I am trying to achieve is a nice way of handling the multiple languages, allowing for the easy addition of an extra language, or an extra field. If I were working in ruby or python, I believe this may be a decent solution;
class RegionalDB:
def __init__(self):
# Maybe connect to a data source such as a db, or files
def set_language(self, lang):
# Verify we have this lang, error if not
# Set an attribute on self for every field entry in this language
text = RegionalDB()
text.set_language('de')
print text.exit_button
The above method would allow me to use the powerful features of python objects being able to dynamically assign members at runtime. The code above would print, in German, the text that was in the "exit_button" field of the data source. In ruby I could do similar, or I could use the attribute_missing function to query the data source directly instead of assigning all variables in memory at once to the object.
Unfortunately C#'s Reflection code is not quite as elegant as the scripts mentioned above. So my issues are as follows;
Creating an elegant, maintainable, regionalisation tool in a compiled, strongly typed language.
Storing the data for the languages in an effective manner using unity
With issue 1. I have been unable to find a design pattern, or much information at all to be honest.
Issue 2. I believe can be solved with some unity editor programming, but I have not touched this topic much and am open to suggestions.
So in question form;
Does anybody know of any design patterns that would work well with C# for regionalisation
Any suggestions on a way forward with an interface/storage mechanism in the unity editor
Thanks.
P.S. I must be able to change language at runtime
There are a lot of C# apps out there which already have l10n support.
I would divide them in three types:
Resources-based internationalization: Texts are stored in XML files. This is the Microsoft way.
PO-based internationalization: Using the long-stablished gettext technology which includes many Unixy tools to deal with updating original texts and translations. You can see how this works by taking a look at the code of some Mono desktop app (i.e.: Banshee), and look for the Catalog.GetString() calls, which use the Mono.Posix assembly as a linked reference.
Vernacular-based internationalization: used in mobile apps to abstract each mobile-platform localization details and have a common method across all.
In my particular opinion, the best and most modern method is the latter. But if you're targetting a desktop app, I guess you can create/contribute a new Vernacular backend to fit this purpose.
We've got a quite complex deployment scenario and want to make use of continuous deployment. Currently we've got a huge MSBuild script for everything, however, MSBuild is ok for building but not really suitable for deploying.
We'd love it if there would be some kind of C# project where we could write the deploy code directly with C#. Is there some C# scripting language which is suitable especially for deploying applications?
While it's not a C# project, you can use my company's product BuildMaster to handle deployments. It's designed to solve some of the problems you're already having, and some you probably don't realize you have (configuration file deployments, database schema updates, process automation, approvals, etc.)
However, if all you want is to write C# scripts for deployment, there's a tool called con-dep which looks like what you're describing.
Well, with Roslyn now I suppose that you can technically now use C# as a scripting language. I don't know if it would be terrible appropriate to do so...
Depending on your deployment solutions you might want to look into the NAntBuilder IDE. We used this for continuous ddeployment together with SVN server. If you have TFS why don't you just look into the details and many possibilities of Build Process templates?
You can use the .NET integrated C# compiler.
The advantage is that you can use the same DLLs/APIs in a C# 'script' as you do in your own software.
I use it in our production software. Our production engineers are able to write some small programs for special stuff.
See this answer for details.
You may want to look into using a tool that's specifically designed for handling deployments. If you're using Team Foundation Server, Team Build + InRelease provides a well-supported, well-documented way to handle your builds and deployments. Now that Microsoft owns InRelease, it'll be part of Team Foundation Server pretty soon.
If not, you can always write PowerShell scripts to handle your deployments. PowerShell isn't C#, but it's built on top of the .NET framework (so all the framework methods you're familiar with are still available), and the learning curve isn't very steep.
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 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 want to run javascript/Python/Ruby inside my application.
I have an application that creates process automatically based on user's definition. The process is generated in C#. I want to enable advanced users to inject script in predefined locations. Those scripts should be run from the C# process.
For example, the created process will have some activities and in the middle the script should be run and then the process continues as before.
Is there a mechanism to run those scripts from C#?
Basically, you have two problems: how to define point of injections in your generated code, and how to run python / ruby / whatev scripts from there.
Depending on how you generate the process, one possible solution would be to add a function to each possible point of injection. The function would check, whether the user has associated any scripts with given points, and if so, runs the script by invoking IronPython / IronRuby (with optionally given parameters).
Disadvantages include: limited accessibility from the scripts to the created process (basically, only variables passed as parameters could be accessed); as well as implementation limits (IronPython's current version omits several basic system functions).
Look into IronPython and IronRuby -- these will allow you to easily interoperate with C#.
You can compile C# code from within a C# application using the CSharpCodeProvider class.
If the compile succeeds you can run the resulting assembly as returned via the CompiledAssembly property of the CompilerResults class.
Awesome C# scripting language - Script.Net
.NET has a scripting language including runtime engine in PowerShell which can be embedded in any .NET application.
You can compile C# code "on the fly" into an in-memory assembly. I think this is possible with IronPython and IronRuby as well. Look at the CodeDomProvider.CreateProvider method.
If you need to run scripts a lot, or if your process runs for a long time, you might want to load these assemblies into another AppDomain. And unload the AppDomain after you're done with the script. Otherwise you are unable to remove them from memory. This has some consequenses on the other classes in your project, because you have to marshall all calls.
Have you thought about Visual Studio for Applications? I haven't heard much about it since .NET 1.1, but it might be worth a look.
http://msdn.microsoft.com/en-us/library/ms974548.aspx
I've done exactly this just recently - allowed run-time addition of C# scripting.
It's not hard at all, and this article:
http://www.divil.co.uk/net/articles/plugins/scripting.asp
is a very useful summary of the details.
One of Microsoft's solutions to JavaScript in C# is ClearScript,
which uses V8, Chrom browser's JavaScript engine. Check its short FAQtorial for code samples.
It has excellent two-way integration - iterator/enumerator, output parameters, optional parameters, parameter arrays, delegate, task/promise/async/await, bigint, and more.
Apart from that, I think the most distinguishing feature is that it does not depend on Rosyln or Dynamic Language Runtime. This can be good or bad - good because there may be a lot less dependencies (depending on your project's target), bad because you need to bundle the native, platform-dependent V8 dll.
If that is ok, you get to enjoy cutting edge JavaScript / ECMAScript. Everything you get on Chrome, or 98% ES6 as of 2022 Feb, plus several extensions. Speed is as fast as Chrome, obviously, so you get the best of both Google and Microsoft.