实例化在C#中的python

实例化在C#中的python

本文介绍了实例化在C#中的python类的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经用Python编写的一类,我想通过IronPython的包装成.NET程序集和实例化一个C#应用程序。我已经迁移类的IronPython,创建库程序集和引用它。现在,我如何真正得到这个类的一个实例?

I've written a class in python that I want to wrap into a .net assembly via IronPython and instantiate in a C# application. I've migrated the class to IronPython, created a library assembly and referenced it. Now, how do I actually get an instance of that class?

类的外观(部分)是这样的:

The class looks (partially) like this:

class PokerCard:
    "A card for playing poker, immutable and unique."

    def __init__(self, cardName):

测试存根我用C#写的是:

The test stub I wrote in C# is:

using System;

namespace pokerapp
{
    class Program
    {
        static void Main(string[] args)
        {
            var card = new PokerCard(); // I also tried new PokerCard("Ah")
            Console.WriteLine(card.ToString());
            Console.ReadLine();
        }
    }
}

我有什么做的,为了在C#中实例化这个类?

What do I have to do in order to instantiate this class in C#?

推荐答案

IronPython的类的没有的.NET类。他们是IronPython.Runtime.Types.PythonType这是Python元类的实例。这是因为Python类是动态的,除了支持在运行时的方法和清除,事情你不能用.NET类做的。

IronPython classes are not .NET classes. They are instances of IronPython.Runtime.Types.PythonType which is the Python metaclass. This is because Python classes are dynamic and support addition and removal of methods at runtime, things you cannot do with .NET classes.

要使用Python类在C#中,您将需要使用ObjectOperations类。这个类可以让你在语言本身的语义上的蟒蛇类型和实例进行操作。例如它使用在适当的时候自动提升整数多头等等神奇的方法你可以找到更多关于ObjectOperations通过查看源代码或使用反射镜。

To use Python classes in C# you will need to use the ObjectOperations class. This class allows you to operate on python types and instances in the semantics of the language itself. e.g. it uses the magic methods when appropriate, auto-promotes integers to longs etc. You can find out more about ObjectOperations by looking at the source or using reflector.

下面是一个例子。 Calculator.py包含一个简单的类:

Here is an example. Calculator.py contains a simple class:

class Calculator(object):
    def add(self, a, b):
        return a + b

您可以使用它从你的pre .NET 4.0 C#code是这样的:

You can use it from your pre .NET 4.0 C# code like this:

ScriptEngine engine = Python.CreateEngine();
ScriptSource source = engine.CreateScriptSourceFromFile("Calculator.py");
ScriptScope scope = engine.CreateScope();

ObjectOperations op = engine.Operations;

source.Execute(scope); // class object created
object klaz = scope.GetVariable("Calculator"); // get the class object
object instance = op.Call(klaz); // create the instance
object method = op.GetMember(instance, "add"); // get a method
int result = (int)op.Call(method, 4, 5); // call method and get result (9)

您将需要引用组件IronPython.dll,Microsoft.Scripting和Microsoft.Scripting.Core。

You will need to reference the assemblies IronPython.dll, Microsoft.Scripting and Microsoft.Scripting.Core.

C#4取得这更简单的的动态类型。

C# 4 made this much easier with the new dynamic type.

ScriptEngine engine = Python.CreateEngine();
ScriptSource source = engine.CreateScriptSourceFromFile("Calculator.py");
ScriptScope scope = engine.CreateScope();
source.Execute(scope);

dynamic Calculator = scope.GetVariable("Calculator");
dynamic calc = Calculator();
int result = calc.add(4, 5);

如果您正在使用Visual Studio 2010或更高版本的NuGet支持只能执行该下载和引用相应的库。

If you are using Visual Studio 2010 or later with NuGet support simply execute this to download and reference the appropriate libraries.

Install-Package IronPython

这篇关于实例化在C#中的python类的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

07-31 04:51