代码之家  ›  专栏  ›  技术社区  ›  Gabriel Isenberg

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

  •  0
  • Gabriel Isenberg  · 技术社区  · 16 年前

    假定以下代码:

    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 回复  |  直到 16 年前
        1
  •  1
  •   sipsorcery    16 年前
    string script = @"
      import clr
      clr.AddReference('FooNamespace')
      from FooNamespace import *
      x = Foo()";
    
    推荐文章