在铁蟒蛇访问净枚举蟒蛇

2023-09-03 21:35:25 作者:证明给你看

我试图访​​问的.Net(C#)在 IronPython的枚举,可以说我们有

I'm trying to access .Net(C#) enums in IronPython, lets say we have

Test.dll的

// Contains Several Enums
enum TestType{..}
enum TestQuality{..}
....
....
enum TestStatus{..}

//Similarly Multiple functions
public void StartTest(TestType testType, TestQuality testQuality){..}
....
....
public TestStatus GetTestStatus(){..}

现在,如果我尝试调用上述功能,我需要选择适当的枚举的参数,到目前为止我所做的就是这一点,

and now if I try to call the above functions, I need to choose the appropriate enum parameters and so far what I did is this,

铁的Python [vs2012]

import clr
clr.AddReference('Test.dll')
from TestDll import *

test = Test()
# Initiate Enums
enumTestType = TestType
enumTestQuality = TestQuality
....
....
enumTestStatus = TestStatus

#Call Functions
test.StartTest(enumTestType.Basic, enumTestQuality.High)
....
....
# goes on

现在上面的IronPython的code正常工作,这里唯一奇怪的一点是,我需要启动所有的枚举(此处Intellisence不工作)之前,我使用它们的功能,这将成为当有更多的困难更枚举使用。而在C#环境(vs2012)我们没有开始,但我们可以用它们马上调用函数时。

now the above IronPython code works fine, the only odd bit here is that I need to initiate all the enums(Intellisence doesnt work here) before I use them with the functions, this will become more difficult when there are more enums to use. whereas in C# environment(vs2012) we dont have to initiate but we can use them straight away when calling functions.

有没有解决这IronPython的更好的方法?

Is there a better way of dealing this in IronPython?

请纠正我,如果我错了,谢谢!

Please correct me if I'm wrong, thanks!

推荐答案

假设枚举都包含在你的测试类,你可以使用它们的完全限定

Assuming the enums are contained within your Test class you can either use them fully qualified

test.StartTest(Test.TestType.Basic, Test.TestQuality.High)

或导入

from TestDll.Test import TestQuality, TestType
test.StartTest(TestType.Basic, TestQuality.High)

如果该枚举都在相同的命名空间测试类就应该没有额外的进口可用的:

If the enums are in the same namespace as the Test class they should be usable without additional imports:

test.StartTest(TestType.Basic, TestQuality.High)