c# - Is it possible run python in .net application? -
in .net application possible save c# code in text file or database string , run dynamically on fly. method useful in many case such business rule engine or user defined calculation engine , etc. here 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 = in enumerable.range(1,100) % 2 == 0 select i; } }"); results.errors.cast<compilererror>().tolist().foreach(error => console.writeline(error.errortext)); } }
the class of primary importance here csharpcodeprovider utilises compiler compile code on fly.
as know python used general-purpose, high-level programming language. design philosophy emphasizes code readability, c# difficult python. it's better use python dynamic code fragments instead c#.
how 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 execute python code in c# .net } }
ironpython implementation of python programming language in .net (c#). after .net version 4.0, ironpython's code can embedded in .net application of dlr (dynamic language runtime).
here's seems reasonable date example of how embed it: http://www.codeproject.com/articles/602112/scripting-net-applications-with-ironpython.
you can read msdn dynamic language runtime , wikipedia additional info on topic.
google full of tutorials on "how embed ironpython in .net".
hope helps!
Comments
Post a Comment