2024-10-03 13:17:11 发布
网友
我正在尝试调用一个特定的属性重载(而不是方法重载),该对象是由IronPython用C#构造的。以下是两种可用的重载:
当我这样称呼它时:
famInst.FromRoom
它返回一个对象IronPython.Runtime.Types.ReflectedIndexer。{cd2>尝试使用异常时抛出:
IronPython.Runtime.Types.ReflectedIndexer
它抛出一个异常"unexpected object "]""。在
"unexpected object "]""
是否有方法指定此属性的重载?在
在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视为一个数组,如下所示:
Foo
在您的案例中,文档包含以下内容:
public Room this[ Phase phase ] { get; }
这意味着FamilyInstance有一个索引器,它接受一个Phase实例并返回一个Room。所以你应该这样使用它:
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具有如下索引器:
String
它接受一个It并在该索引处返回一个char,或者如果该索引超出范围,则抛出异常。在
char
在C#中不存在属性重载。你要问的是一个索引器。下面是一个索引器的示例:
这意味着我可以将
^{pr2}$Foo
视为一个数组,如下所示:在您的案例中,文档包含以下内容:
这意味着
FamilyInstance
有一个索引器,它接受一个Phase
实例并返回一个Room
。所以你应该这样使用它:或者,由于它是命名索引器,因此可以执行以下操作:
另一个是普通属性,记录如下:
您只能访问该属性,但不能这样设置:
因此,基本上,indexer允许您将类视为数组,即使类不是数组。在这种情况下,
^{8}$FamilyInstance
可以像数组一样处理。在.NET中有许多类型具有索引器,例如,String
具有如下索引器:它接受一个It并在该索引处返回一个
char
,或者如果该索引超出范围,则抛出异常。在相关问题 更多 >
编程相关推荐