C# 通过接口访问IronPython.NET类
我使用IronPython从正在运行的应用程序访问.net(C#)类实例,但我试图使用接口将访问限制为仅访问类中的方法和属性。这不起作用,因为我的Python可以访问所有公共方法和属性 我已经创建了一个小的测试应用程序来演示这个问题,并尝试可能的解决方案,但到目前为止,它与我更大的生产应用程序存在相同的问题 我的类实例通过ScriptScope.SetVariable(“TestApp”,this)与Python共享,这很好 相关的C#代码如下:C# 通过接口访问IronPython.NET类,c#,interface,ironpython,C#,Interface,Ironpython,我使用IronPython从正在运行的应用程序访问.net(C#)类实例,但我试图使用接口将访问限制为仅访问类中的方法和属性。这不起作用,因为我的Python可以访问所有公共方法和属性 我已经创建了一个小的测试应用程序来演示这个问题,并尝试可能的解决方案,但到目前为止,它与我更大的生产应用程序存在相同的问题 我的类实例通过ScriptScope.SetVariable(“TestApp”,this)与Python共享,这很好 相关的C#代码如下: using System; using Micr
using System;
using Microsoft.Scripting.Hosting;
using System.Collections.Generic;
using IronPython.Hosting;
namespace PythonTestObj
{
public class PythonScriptRun
{
// Bunch of Python code here. Not relevent
public ITest GetProp()
{
ITest retVal = (ITest) _TestClass;
return retVal;
}
public interface ITest
{
string HelloWorld { get; }
}
public class TestClass : ITest
{
public string HelloWorld
{
get
{
return "Hello World";
}
}
public string GoodbyeWorld
{
get
{
return "Goodbye World";
}
}
}
}
}
以下是我的Python脚本的全部内容:
import System
import clr
import sys
clr.AddReference("PythonTestObj.dll")
from PythonTestObj import *
engOp = globals().get('TestApp')
#engOp now refers to an instance of TestClass through an ITest interface
# This print should work as HelloWorld is in the ITest
retStr = engOp.GetProp().HelloWorld
print retStr
# This should fail because GoodbuyWorld is not in ITest
retStr = engOp.GetProp().GoodbyeWorld
print retStr
运行此python脚本时,我得到以下输出:
Hello World
Goodbye World
由于GetProp()方法返回了一个ITest接口,我不知道它如何找到GoodbyeWorld属性
问题:有没有办法做到这一点,还是我找错了方向