将用户定义的类型从IronPython传递给c#

本文关键字:IronPython 用户 定义 类型 | 更新日期: 2023-09-27 18:10:51

. NET库(.dll)有许多用户定义的枚举类型,我希望将它们作为参数传递,例如

    public enum fieldType_e
    {
        FIELDTYPE_NONE = 0,   // 0 - Field 
        FIELDTYPE_1,          // 1 - Field 
        FIELDTYPE_2,          // 2 - Field 
        FIELDTYPE_3,          // 3 - Field 
        FIELDTYPE_LAST
    }

到成员函数:

    public bool CMD_AddUser(int UserNumb, String PrimaryField, fieldType_e FieldType )
    {
    .
    .
    .
    }

例如,当我从IronPython调用CMD_AddUser( 1, '2222', 3)时,它接受前两个参数(int和string),但拒绝类型fieldType_e

下面是一个示例IronPython会话:
C:'>ipy
IronPython 2.7.5 (2.7.5.0) on .NET 4.0.30319.34209 (32-bit)
Type "help", "copyright", "credits" or "license" for more information.
>>> import clr
>>> clr.AddReferenceToFileAndPath('Library.dll')
>>> import Library.Protocols.R3
>>> type(Library.Protocols.R3)
<type 'namespace#'>
>>> from Library.Protocols.R3 import R3Serial
>>> from Library.Protocols.R3 import R3_App
>>> 
>>> # Instantiate Library.Protocols.R3 classes
>>> App = R3_App()
>>> R3_Serial = R3Serial("COM3", 115200, 0)
>>> R3_Serial.App.CMD_Handshake()
True
>>> R3_Serial.App.CMD_AddUser( 1, '2222', 3)
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
TypeError: Cannot convert numeric value 3 to fieldType_e.  The value must be zero.

IronPython识别enum fieldType_e和元素FIELDTYPE_NONE,但当我尝试使用它时抛出AttributeError。

>>> AHG_Library.fieldType_e.FIELDTYPE_NONE
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
AttributeError: 'namespace#' object attribute 'fieldType_e' is read-only

将用户定义的类型从IronPython传递给c#

您传递的是integer(3)而不是enum fieldType_e。FIELDTYPE_3