在IronPython中实例化自定义.NET类型

时间:2009-04-10 05:18:06

标签: c# .net ironpython

假设以下代码:

public class Foo
{
    public string Bar { get; set; }
}

如何在以下代码中实例化Foo实例?

ScriptRuntimeSetup setup = Python.CreateRuntimeSetup(null);
ScriptRuntime runtime = new ScriptRuntime(setup);
ScriptEngine engine = Python.GetEngine(runtime);
ScriptScope scope = engine.CreateScope();

string script = @"x = ***???***";
ScriptSource source = engine.CreateScriptSourceFromString(script, SourceCodeKind.Statements);
source.Execute(scope);

1 个答案:

答案 0 :(得分:1)

string script = @"
  import clr
  clr.AddReference('FooNamespace')
  from FooNamespace import *
  x = Foo()";