从Python调用C#中的特定属性重载

2024-10-03 13:17:11 发布

您现在位置:Python中文网/ 问答频道 /正文

我正在尝试调用一个特定的属性重载(而不是方法重载),该对象是由IronPython用C#构造的。以下是两种可用的重载:

enter image description here

当我这样称呼它时:

famInst.FromRoom

它返回一个对象IronPython.Runtime.Types.ReflectedIndexer。{cd2>尝试使用异常时抛出:

^{pr2}$

它抛出一个异常"unexpected object "]""。在

是否有方法指定此属性的重载?在


Tags: 对象方法属性objectruntimetypesironpythoncd2
1条回答
网友
1楼 · 发布于 2024-10-03 13:17:11

在C#中不存在属性重载。你要问的是一个索引器。下面是一个索引器的示例:

public class Foo
{
    SomeObject this[int index]
    {
        // The get accessor.
        get
        {
            // return the value specified by index
        }

        // The set accessor.
        set
        {
            // set the value specified by index
        }
    }
}

这意味着我可以将Foo视为一个数组,如下所示:

^{pr2}$

在您的案例中,文档包含以下内容:

public Room this[
    Phase phase
] { get; }

这意味着FamilyInstance有一个索引器,它接受一个Phase实例并返回一个Room。所以你应该这样使用它:

// If phase is abstract then new the concrete type. I am not familiar with revit API
var somePhase = new Phase(); 
famInst[somePhase];

或者,由于它是命名索引器,因此可以执行以下操作:

famInst.FromRoom[somePhase];

另一个是普通属性,记录如下:

public Room FromRoom { get; }

您只能访问该属性,但不能这样设置:

Room room = famInst.FromRoom;

因此,基本上,indexer允许您将类视为数组,即使类不是数组。在这种情况下,FamilyInstance可以像数组一样处理。在.NET中有许多类型具有索引器,例如,String具有如下索引器:

^{8}$

它接受一个It并在该索引处返回一个char,或者如果该索引超出范围,则抛出异常。在

相关问题 更多 >