Question

In .NET application is possible save C# code in text file or database as string and run dynamically on the fly. This method is useful in many case such as business rule engine or user defined calculation engine and etc. Here is a nice example:

using System;
using System.Collections.Generic;
using System.Linq;
using Microsoft.CSharp;
using System.CodeDom.Compiler;

class Program
{
    static void Main(string[] args)
    {
        var csc = new CSharpCodeProvider(new Dictionary<string, string>() { { "CompilerVersion", "v3.5" } });
        var parameters = new CompilerParameters(new[] { "mscorlib.dll", "System.Core.dll" }, "foo.exe", true);
        parameters.GenerateExecutable = true;
        CompilerResults results = csc.CompileAssemblyFromSource(parameters,
        @"using System.Linq;
            class Program {
              public static void Main(string[] args) {
                var q = from i in Enumerable.Range(1,100)
                          where i % 2 == 0
                          select i;
              }
            }");
        results.Errors.Cast<CompilerError>().ToList().ForEach(error => Console.WriteLine(error.ErrorText));
    }
}

The class of primary importance here is the CSharpCodeProvider which utilises the compiler to compile code on the fly.

As you know Python is a widely used general-purpose, high-level programming language. Its design philosophy emphasizes code readability, but C# is difficult that python. So it's better use python for dynamic code fragments instead C#.

How to execute python dynamically in C# application?

class Program
{
    static void Main(string[] args)
    {
        var pythonCode = @"
                a=1
                b=2
                c=a+b
                return c";
        //how to execute python code in c# .net
    }
}
Was it helpful?

Solution

IronPython is an implementation of the Python programming language in .NET (C#). After .NET version 4.0, IronPython's code can be embedded in .NET application with the help of the DLR (Dynamic Language Runtime).

Here's what seems a very reasonable up to date example of how to embed it: http://www.codeproject.com/Articles/602112/Scripting-NET-Applications-with-IronPython.

You can also read the MSDN For Dynamic Language Runtime and its Wikipedia to get additional info on the topic.

Google is also full of tutorials on "How to embed IronPython in .NET".

Hope this helps!

OTHER TIPS

IronPython is an open-source implementation of the Python programming language which is tightly integrated with the .NET Framework. IronPython can use the .NET Framework and Python libraries, and other .NET languages can use Python code just as easily.

from http://ironpython.net/

and there are tools to integrating with visual studio in their site. and if your aim is to use Reflection and compile python code in run-time (like using of CompileAssemblyFromSource method) you can see this: http://social.msdn.microsoft.com/Forums/vstudio/en-US/9c72c404-eb6c-468b-bdbc-a6a91c55cdfe/c-ironpython-reflection?forum=csharpgeneral

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top