Skip to main content

Posts

Showing posts from September, 2010

C# As a Scripting Language in your .NET Applications – Using Mono’s Compiler As a Service

Note: This is the third article in the series. In the first two articles, I explainedHow To Host Mono’s C# Compiler as a Service in .NET for Run time code evaluationDynamic Filtering and Querying in Your .NET applications - Using Mono’s C# Compiler As ServicePrefaceIn this post, we’ll see how to use C# as a scripting language in your .NET applications. Let us start with a very minimal game application, where you have two players, and your users can control these players by writing some script.In the previous post, I explained how to create a delegate/predicate from Mono’s Evaluator and pass it back to the Main application, so that we can use the predicate for custom filtering operations. Now, What is interesting in below code is, how we are passing the context information from the main application to Mono’s Evaluator, so that the objects from the main applications can be accessed via Mono’s Evaluator for scripting.C# For ScriptingSo here we go. Have a look at this entire implementati…

Dynamic Filtering and Querying in Your .NET applications - Using Mono’s C# Compiler As Service

In my last post, I explained how to host Mono’s C# Compiler As A Service in your .NET applications. In this post, we’ll explore this a bit further, and see how to leverage Mono’s compiler as a service for implementing some dynamic querying features in your .NET applications. Read my first post How To Host Mono’s C# Compiler as a Service in your .NET Application if you missed it. Now, let us explore some more points about using Mono’s C# Compiler as a Service in general, and the ‘Evaluator’ class in particular, to see how to leverage this for some practical uses like dynamic data filtering.The Mono.CSharp.Evaluator ClassLet us consider a simple and dirty example. Fire up a Console application in Visual Studio, Add a reference to Mono.CSharp.dll, and try this code. Have a look at the Init method, and Run method of the Mono.CSharp.Evaluator classusing System; using System.Collections.Generic; using System.Linq; using System.Text; using Mono.CSharp; namespace MonoHost { class Program…

How To Host Mono’s CSharp Compiler as a Service in .NET - For Runtime Code Evaluation/REPL

Anders in his C# Futures Talk mentioned about C# Compiler as a Service and demonstrated an REPL (Read Evaluation Print Loop) implementation. If this is going to be a part of C#/.NET framework, this will allow you to execute plain C# code strings during runtime. This will allow you to implement features like interactive scripting in your applications. As of now, .NET/C# stack don’t have this. Enter Mono’s C# Compiler as a service implementation. Big Bang. Last April, Miguel announced the availability of Mono’s C# Compiler as a Service assembly, along with a neat REPL C# shell. From that point onwards, I was thinking about writing a quick post on some of my thoughts about hosting Mono’s assembly in your .NET applications, so here we go.1 – Download the C# Shell and Mono.CSharp.dll assemblyA snapshot of the C# REPL shell and Mono.CSharp.dll is available from this direct link (From Miguel’s post). The Zip contains a C# REPL shell - csharp.exe – and also the Mono.CSharp.dll which is the Co…

7 Freely available E-Books/Guides I found essential for .NET Programmers and Architects

A lot of people ask me for good book recommendations. Hence, for some time, I was thinking about compiling a quick list of useful free E-Books for .NET programmers and budding architects. Most of us start our programming career as a ‘casual’ programmer – My own definition about a ‘Casual’ coder is a guy who can code, but who is least concerned/interested about the beauty, efficiency, extensibility and readability of the code he writes. Some of these books will definitely help us to evolve our coding skills and thought processes for developing better solutions. Hope you find this list useful, here we go.Foundations Of ProgrammingThe Foundation Of Programming Series Free e-book By Karl Seguin is one of my favorites. It is simple, short and sweet. Especially for ‘casual’ programmers, this will give a better thought process – that’ll definitely enable them to code better and think better. This book covers the ALT.NET Philosophy, Domain Driven Development concepts, DI, TDD etc in a nice wa…

Revisiting Few C# Concepts - Delegates, Anon Methods, Lambdas, Expression Trees

Let us explore few concepts about Delegates, Anonymous methods, Lambdas and Expression Trees. This is an introductory post, and I’ll follow up with more posts. These code examples are pretty simple, you can fire up a C# Console application in Visual Studio and try these examples if you are interested.DelegatesWe know that a delegate is a type that can reference a method. For example, consider this example to define a delegate and create a delegate instance. //Example 1 - Creating a delegate instance using Named method public delegate int BinaryOperationDelegate(int x,int y); class Program { static void Main(string[] args) { //Create a delegate instance using named method var add = new BinaryOperationDelegate(AddOperation); int result = add(20, 30); Console.WriteLine(result); } static int AddOperation(int x,int y) { return x+y; } }The above code will give …