Category Archives: C#

Hosting IronPython in a C# application

Let’s use IronPython as a scripting language within our C# application.

Note: an application I work on uses an earlier version of IronPython and they’ve significantly changed the way you use it, so this information is only relevant for version 2.7.x of Python

What are we trying to do here ?

So, the premise is this. I have a C# application and I want to allow a user/ops or whoever to write scripts which can interact directly with the running instance of the application and/or the types within an assembly. Standard scripting fair I’m sure we’d all agree.

Let’s start off by downloading the package IronPython via NuGet. The version installed is 2.7.4.

To create the Python scripting engine we use the following

ScriptEngine engine = Python.CreateEngine();

We could alternatively create a runtime and then get the engine from it, as per

ScriptRuntime runtime = Python.CreateRuntime();
ScriptEngine engine = runtime.GetEngine("Python");

Now, unlike earlier versions of the scripting engine, it appears (unless I find information that differs from this view) that we no longer really have a global scope for writing our scripts but instead create a scope ourselves (i.e. in earlier versions we could add variables and execute code on what appeared to be a global level).

So we need to create a script scope using

ScriptScope scope = engine.CreateScope();

Now we can interact with this scope, importing modules, executing code and adding variables.

Allowing our scripts to use our assembly types

Let’s assume our application adds a bunch of types/classes that we might instantiate from IronPython. To make these available to the Python code we need to use the CLR from Python to add references to our assemblies, but in this instance we don’t want the script writer to have to do this every time, so we write

scope.ImportModule("clr");

engine.Execute("import clr", scope);
engine.Execute("clr.AddReference(\"MyAssembly\")", scope);
engine.Execute("from MyAssembly import *", scope);

In the above we import the CLR module into the scope object and then execute some Python code using the engine, against the scope we created. As eluded to, we could have written these commands in every Python script as per the code below

import clr
cl.AddReference("MyAssembly")
from MyAssembly import *

but I think you’d agree it’s much better to let the host setup this code instead, especially if we wanted to add lots of assemblies.

Now from Python we can instantiate classes from MyAssembly. For example, if my host application included an assembly called MyAssembly, with the following class definition

public class MyLibrary
{
   public void Run()
   {
      Console.WriteLine("MyLibrary run called");
   }
}

then, from Python we could write the following

r = MyLibrary()
r.Run()

Putting this altogether to show how we can reference an assembly and create the types within the said assembly, just create a console application project called ConsoleApplication and enter the following

public class Application
{
   public string Name { get { return "MyApp"; } }
}

class Program
{
   static void Main(string[] args)
   {
      string code = "app = Application()\n" +
                    "print app.Name";

      ScriptEngine engine = Python.CreateEngine();
      ScriptScope scope = engine.CreateScope();

      scope.ImportModule("clr");

      engine.Execute("import clr", scope);
      engine.Execute("clr.AddReference(\"ConsoleApplication\")", scope);
      engine.Execute("from ConsoleApplication import *", scope);

      ScriptSource source = engine.CreateScriptSourceFromString(code, 
                     SourceCodeKind.Statements);
      source.Execute(scope);
   }
}


What if we already have an instance of an object in our hosting application that we want to make available to IronPython ?

So in this instance we use the following, from the hosting C# application

scope.SetVariable("host", this);

where, in this example, we pass in an instance of the hosting application. Now from Python we can call methods on the variable host, i.e. assuming the hosting application has a method named ShowMessage, we could write the following

host.ShowMessage("Hello from Python")

Finally, let’s say we have everything setup and we have a text editor embedded in the hosting application with a run button, when run is pressed we want to run the code entered into the text editor, thus

var source = engine.CreateScriptSourceFromString(code, SourceCodeKind.Statements);
source.Execute(scope);

the above code takes the source code (the code variable), then executes it in the given scope.

We could load the script from a file if we wished using

engine.CreateScriptSourceFromFile(filename);

where filename is string representing the location and filename of the Python script file.

Putting this all together into a simple example, just create a console application project and enter the following code

public class Application
{
   public string Name { get { return "MyApp"; } }
}

class Program
{
   static void Main(string[] args)
   {
      string code = "print application.Name";

      ScriptEngine engine = Python.CreateEngine();
      ScriptScope scope = engine.CreateScope();

      scope.SetVariable("application", new Application());

      ScriptSource source = engine.CreateScriptSourceFromString(code, 
                     SourceCodeKind.Statements);
      source.Execute(scope);
   }
}

Using .NET reflection to dynamically create methods with generic parameters

Following on from my post Dynamically creating C# code using the CodeDomProvider I’ve created a new type dynamically using the CodeDomProvider and I now need to use the type within a generic method call.

So to start with, I have a DataSerializer class (the basics are shown below – I’ve just distilled it down to the method signature that we’re interested in for this post)

public class DataSerializer<T> where T : new()
{
   public static IList<T> Deserialize(IDelimiterSeperatedReader dsr, 
              string data, DelimitedDeserializeOptions options)
   {
      // method implementaion
   }
   // other methods
}

From the previous post I have a dynamically created type (so I just have a Type object, the variable generatedType shown in the code below). I need to create a call to the static method (Deserialize) with this type as a type generic parameter.

Type generatedType = cr.CompiledAssembly.GetType("CsvGeneratedData");

Type[] arguments = { typeof(IDelimiterSeperatedReader), 
                  typeof(string), typeof(DelimitedDeserializeOptions) };

Type dataserializer = typeof (DataSerializer<>);
Type typedSerializer = dataserializer.MakeGenericType(generatedType);
MethodInfo mi = typedSerializer.GetMethod("Deserialize", 
               BindingFlags.Public | BindingFlags.Static, 
               null, arguments, null);

In the above the arguments variable is an array of Type’s in the order of the arguments the method Deserialize takes. We then use

This code creates the MethodInfo object but we now need to invoke the method itself, so we use the following Type dataserializer = typeof (DataSerializer<>) to create a DataSerializer but at this point it does not have the generic parameter assigned to it, so we then use MakeGenericType to assign the generic parameter to the dataserializer. Finally we get the method info. for the static method we want to call.

Now we need to invoke the method using the following

mi.Invoke(null, parameters);

where parameters is an object array with the relevant method arguments.

Dynamically creating C# code using the CodeDomProvider

I’ve been working on some code which generates C# classes. Basically it creates POCO classes to allow me to serialize/deserialize data from a data file into the POCO classes. The data file format can change and so this code allows me to easily regenerate the classes I need. This works fine, but I thought it might be better if I could create the code on the fly and use in without having to recompile the application (that would otherwise include the compiled source). In other words I wanted to create the C# classes and then generate the types and use them all dynamically at runtime.

.NET includes the CodeDomProvider class which can easily be used to create an assembly with our types created on the fly.

Here’s the code

CodeDomProvider provider = CodeDomProvider.CreateProvider("CSharp");

CompilerParameters cp = new CompilerParameters();
cp.GenerateInMemory = true;
cp.TreatWarningsAsErrors = false;
cp.ReferencedAssemblies.Add("putridparrot.Data.dll");

CompilerResults cr = provider.CompileAssemblyFromSource(cp, source);

The above is pretty self explanatory, but let’s go through it anyway.

First off, we create a CodeDomeProvider specifying the provider we require for C# code. We can (currently) create code dom’s for the following languages

  1. CSharp – C#
  2. VisualBasic – VB.NET
  3. JScript – JavaScript
  4. Cpp – C++

Next we create the compiler parameters. In this instance we want the code generated in memory, as opposed to using a file and I don’t want “warnings as error’s” in this instance. I do however need to add any of my referenced assemblies. In this case the code uses one external assembly, the putridparrot.Data.dll (as the generated code uses attributes from this assembly to mark the properties of the POCO for serialization/deserialization).

Finally we compile the code into the assembly from the source code supplied (in this case the string source contains the C# class to be compiled). The CompilerResults may include errors, so we can check this by using the following

cr.Errors.Count

Assuming we’ve no errors, then we can now access a compiled type by interacting with the assembly created by the CodeDomProvider, for example

Type type = cr.CompiledAssembly.GetType("AutoGeneratedDataType");

The CompiledAssembly is just a standard Assembly at this point, so obviously you can do all the standard things you can do on any other Assembly object.

Side note

As a side note we can iterate over the languages supported via the CodeDomProvider using the following

CompilerInfo[] compilerInfo = CodeDomProvider.GetAllCompilerInfo();
foreach (var ci in compilerInfo)
{
   string[] languages = ci.GetLanguages();
   foreach(var language in languages)
   {
      Console.WriteLine(language);
   }
}

Beware an exception in a ReactiveCommand

The Reactive UI class ReactiveCommand can be used to implement a view model’s commands as per the following:

Synchronize = ReactiveCommand.Create(_ => true, DoSynchronize);

This is excellent and works like RelayCommand and DelegateCommand from other MVVM frameworks, in that the first argument of the Create method handles the CanExecute and the second is the Action to be executed when ICommand.Execute is called.

One problem this usage has, is in the case of an exception within the command’s action. As Reactive UI uses Reactive Extensions it also handles errors in the same way.

When an error occurs in Reactive Extensions the default behaviour after an OnError is that subscribers are unsubscibed, in other words no further calls are made to your subscribed Action.

In the case of ReactiveUI’s ReactiveCommand this means if an exception occurs during the Action the command essentially disconnects itself and further commands are ignored. So a simpler way of solving this is to use ReactiveAsyncCommand.

Synchronize new ReactiveAsyncCommand(null);
Synchronize.RegisterAsyncAction(_ => DoSynchronize());

This way a new IObservable is created each time a command is executed, thus an exception will simply stop the current IObservable and the next time the command is called a new one is created.

Indexing your MongoDB data

By default MongoDB creates an index on the _id (ObjectId) field, but we can easily add indexes to other fields.

Using the JavaScript shell

In the JavaScript shell simply call ensureIndexUsing the 10gen drivers in C#

In C# using the 10gen drivers we can create an index using the following

collection.EnsureIndex(new IndexKeysBuilder().Ascending("artist"));

where collection is

MongoCollection<CD> collection = db.GetCollection<CD>("cds");

To remove an index we can simply use

collection.DropIndex(new IndexKeysBuilder().Ascending("Artist"));

Handling case-sensitive mapping from MongoDB to a POCO

So the convention appears to be to use camel case for column/field names within MongoDB. For example if we create an entry such as db.cds.Update({artist:”Alice Cooper”}).

In C# the convention is for properties, for example, to be written in Pascal case. So we’d have something like

public class CD
{
   public ObjectId Id { get; set; }
   public string Artist { get; set; }
   public string Title { get; set; }
   public string Category { get; set; }
}

So obviously MongoDB has a field name artist and we need to map it to the property name “Artist”.

To handle this we can use the BsonElement in the MongoDB.Bson.Serialization.Attributes namespace, as per

public class CD
{
   public ObjectId Id { get; set; }
   [BsonElement("artist")]
   public string Artist { get; set; }
   [BsonElement("title")]
   public string Title { get; set; }
   [BsonElement("category")]
   public string Category { get; set; }
}

or we can set up the mappings using the following

BsonClassMap.RegisterClassMap<CD>(cm =>
{
   cm.AutoMap();
   cm.GetMemberMap(c => c.Artist).SetElementName("artist");
   cm.GetMemberMap(c => c.Title).SetElementName("title");
   cm.GetMemberMap(c => c.Category).SetElementName("category");
});

Note: we do not need to setup the Id field to any mapping as this appears to be mapped based upon it’s type.

A class map may only be registered once, we can use BsonClassMap.IsClassMapRegistered if need be to ensure this.

More information can be found at Serialize Documents with the CSharp Driver

Creating a simple database in MongoDB with C#

This is quick post on getting up and running with MongoDB using the “Official MongoDB C# driver” and both creating a database and adding some data to it.

  1. Using Nuget, add the “Official MongoDB C# driver” from 10gen to your project
  2. Add the following using clauses
    using MongoDB.Bson;
    using MongoDB.Driver;
    
  3. Create a POCO object to represent the data you want to persist. The key thing to remember is to add a property of type ObjectId without this we’ll get and exception stating “No IdGenerator found”.

    So an example POCO might look like this

    public class Person
    {
       public ObjectId Id { get; set; }
       public string FirstName { get; set; }
       public string LastName { get; set; }
       public int Age { get; set; }
    }
    
  4. Now we need to connect to the server and create/use a database.

    MongoClient client = new MongoClient();
    MongoServer server = client.GetServer();
    MongoDatabase db = server.GetDatabase("MyDatabase");
    

    Obviously the first two lines create a mongo client and then gets access to the server. The line server.GetDatabase(“MyDatabase”) will get the database (if it exists) but also create a database if it doesn’t exist.

    Note: if you are creating a database using GetDatabase it will not exist until you actually store data in it.

  5. Next we’re going to assume we want to store a collection of employees (a collection of Person objects). So we want to get the collection of “employees”. Like the creating of the database, if no employees currently exist we still get a collection object which we can then save data to.

    MongoCollection<Person> collection = db.GetCollection<Person>("employees");
    
  6. Let’s now create a Person object ready for adding to the collection and ultimately to the database.

    Create the following

    Person p = new Person
    {
       Id = ObjectId.GenerateNewId(),
       FirstName = "Bob",
       LastName = "Baker",
       Age = 36
    }
    

    Notice that we generate the Id using ObjectId.GenerateNewId().

  7. Our next step is to save the new Person to the collection and this will add the data to the collection and thus the database, thus we can then query for this data afterwards using the JavaScript shell.

    collection.Save(p);
    

Attributes on return values in C#

What’s the C# syntax for applying an attribute to a return type ?

[return: SomeAttribute()]
public string GatValue()
{
    return"Some Value";
}

Here’s a simple example of a custom attribute used for return types

[AttributeUsage(AttributeTargets.ReturnValue, AllowMultiple = false)]
class MyAttribute : Attribute
{
   public string Description { get; set; }
}

public class SomeClass
{
   [return: MyAttribute(Description = "A Return Value")]
   public string GetValue()
   {
      return "Hello World";
   }
}

class Program
{
   static void Main(string[] args)
   {
      object[] attributes = typeof (SomeClass).
                  GetMethod("GetValue").
                  ReturnTypeCustomAttributes.
                  GetCustomAttributes(false);
   }
}

This will return a single item in the object[] of type MyAttribute.

MemoryCache, caching in .NET 4

Having used the Enterprise Application Blocks in the past I’ve been well aware of the capabilities of the caching block, but recently was looking for some caching for a small application I have and stumbled across the MemoryCache which looks like it’s the replacement for the Caching Block.

It supports caching policy’s such as absolute expiration and sliding expiration.

You can create multiple instances of a MemoryCache or use a singleton in the form of MemoryCache.Default in the typical scenarios where only a single instance of the cache is required.

A simple example is shown below

MemoryCache cache = MemoryCache.Default;

cache.Add("Key", "Value", new DateTimeOffset(DateTime.Now.AddSeconds(10));
object o = cache.Get("Key");

In the above (fairly useless) example we get the default instance of the cache and then add a key and value to the cache with an absolutely expiry in 10 seconds time. We then get the item from the cache and ten seconds later it will have been removed from the cache.

When I get around to it I’ll amend with some better examples.

Beware of using RegexOptions.Compiled Regex in some situations

Using the RegexOptions.Compiled option in a Regex instance means that the regular expression is compiled into IL upon creation, which sounds good.

But beware, it can be an expensive operation which highlights itself if the code is called many times. If the Regex instance is reused a lot it makes sense to use this option, but even then beware that the time to create the compiled expression might not be worth it in a one-off scenario. In such cases do not use RegexOptions.Compiled.

Let’s look at some results of a simple test.

In the first example I’m simple creating the Regex in a method and calling the Split method on it, so these results obviously include the creation and use of the object. “Option” denotes whether the Regex was compiled or interpreted and the number column denotes is the number iterations calling we make creating a Regex and using it (time is shown in ms)

[table “” not found /]

Next we’ll create the Regex and reuse it

[table “” not found /]

Obviously what you see in the second table is that when the Regex Split is called a low number of times the performance gain of compiling the expression is minimal, but it gets progressively better the more times the regex is used.

So the lesson is to not use the RegexOptions.Compiled option unless you’re pretty sure the performance hit of creating the Regex isn’t an issue and it will be called a large number of times. All other times do not used RegexOptions.Compiled.

Note: performance tests were carried out on a 32-bit Windows XP machine