简体   繁体   中英

IronPython C# linkage

Can anyone tell me, how to use methods from C# in ironpython?

I know some basics but don't understand what to do if I don't want to build my C# project as .dll?

Code for the ConsoleApplication1.exe:

 using System;
    using IronPython.Hosting;
    using Microsoft.Scripting.Hosting;

        namespace ConsoleApplication1 {
            class Program
            {
                static void Main()
                {
                    ScriptEngine engine = Python.CreateEngine();
                    engine.ExecuteFile("C:/Users/Vlad/Desktop/kal.py");
                }

                public int Plus(int a, int b)
                {
                    int z = a + b;
                    Console.WriteLine(z);
                    return z;
                }
            }
    }

//kal.py

print 'test1'
import clr
clr.LoadAssemblyFromFile("C://Users/Vlad/Documents/Visual Studio 2015/Projects/ConsoleApplication1/ConsoleApplication1/bin/Debug/ConsoleApplication1.exe")
import ConsoleApplication1
arith = ConsoleApplication1.Program.Plus(10,20)
print arith

just change type of C# application to Class library and reference it to your python app.
from solution explorer right click on C# project note and click Properties and change appliction type to class library.
you can reference a library to the project from Project>add reference menu.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM