2013-06-17 7 views
3

이것은 내가 알 수없는 단순한 오류가있는 것처럼 보입니다. C# WPF 응용 프로그램에서 IronPython을 사용하고 있고 사용자 정의 C# 클래스에서 함수를 실행하려고하면 다음 오류가 발생합니다. AttributeError: 'MyScriptingFunctions' object has no attribute 'Procedure'.IronPython - "AttributeError : 객체에 속성이 없습니다."사용자 정의 클래스

내가 실행중인 python 스크립트는 매우 간단하고 두 줄이 있습니다.

 private void btnRunScript_Click(object sender, RoutedEventArgs e) 
    { 
     MyScriptingFunctions scriptFuncs = new MyScriptingFunctions(); 
     ScriptEngine engine = Python.CreateEngine(); 
     ScriptScope scope = engine.CreateScope(); 

     ScriptRuntime runtime = engine.Runtime; 
     runtime.LoadAssembly(typeof(String).Assembly); 
     runtime.LoadAssembly(typeof(Uri).Assembly); 

     //Set Variable for the python script to use 
     scope.SetVariable("txt", fullReadResultsTextBox); 
     scope.SetVariable("MyFunc", scriptFuncs); 
     string code = this.scriptTextBox.Text; 
     try 
     { 
      ScriptSource source = engine.CreateScriptSourceFromString(code, SourceCodeKind.Statements); 
      source.Execute(scope); 
     } 
     catch (Exception ex) 
     { 
      ExceptionOperations eo; 
      eo = engine.GetService<ExceptionOperations>(); 
      string error = eo.FormatException(ex); 

      MessageBox.Show(error, "There was an Error"); 
      return; 
     } 

    } 

단순히 설정하고 :

여기
class MyScriptingFunctions 
{ 
    public MyScriptingFunctions() {} 

    public void Procedure(int num) 
    { 
     Console.WriteLine("Executing procedure " + num); 
    } 
} 

가 어떻게 설정 IronPython의 엔진입니다 : 1 호선 오류가 줄에 2

txt.Text = "some text" 
    MyFunc.Procedure(5) 

MyScriptingFunctions.cs 발생, 미세 실행 2 개의 변수 : System.Windows.Controls.TextBoxtxt 및 내 사용자 정의 클래스 MyScriptingFunctions의 대상인 MyFunc입니다.

내가 뭘 잘못하고 왜 파이썬 스크립트가 TextBox 메서드를 올바르게 실행하고 사용자 정의 클래스의 메서드를 실행하지 않습니까?

답변

6

문제가 있거나 단지 복사 붙여 넣기 오류 만 보일 수 있다고 생각하면 public이 아닌 MyScriptingFunctions입니다. 그 이 인스턴스를 전달하기 때문에 문제가되지 않아야합니다. 클래스를 가져 오려고하지는 않지만 시도해 볼 가치가 있습니다. 그렇지 않으면 모든 것이 잘 보입니다.

+0

그 트릭을했습니다. 매우 실망스러운 작은 오류. – McLovin

+2

동일한 문제가 있었지만 수업을 공개로 생각하지 않았습니다. 이 문제에 얽매여있는 다른 사람들 : 수업은 공개 일 필요는 없지만 ** 방법은 회의 밖에서 볼 수있을 정도로 공개되어야합니다 **. 예를 들어, 파이썬이 인터페이스 메소드만을 호출하는 한, 공용 인터페이스를 구현하는 내부 클래스를 가질 수 있습니다. –

+0

1 분 전에이 문제를 확인할 수 있습니다. 감사. –

관련 문제