1

I am trying to call a specific property overload (not method overload) on an object constructed in C#, from IronPython. Here are the two overloads available:

enter image description here

When I call it like so:

famInst.FromRoom

it returns an object IronPython.Runtime.Types.ReflectedIndexer. When I try to use the Overloads it throws an exception:

famInst.FromRoom.Overloads[]()

it throws an exception "unexpected object "]"".

Is there a way to specify the overload for this property?

ivamax9
  • 2,601
  • 24
  • 33
konrad
  • 3,544
  • 4
  • 36
  • 75
  • please give me the link to that documentation – CodingYoshi Dec 27 '16 at 18:22
  • Try this: http://www.revitapidocs.com/2017/ea6bc434-d938-d0e5-ecc3-33e37dbf1d1b.htm – konrad Dec 27 '16 at 19:20
  • ok, it looks like there is an issue with the Revit API and IronPython implementation of it. This was already discussed here: http://stackoverflow.com/questions/39001482/revitapi-ironpython-toroom-returns-indexer-object It seems that there is no answer to it just yet. – konrad Dec 27 '16 at 20:54

1 Answers1

1

There is no such thing as property overloading in C#. What you are asking about is called an indexer. Here is an example of an indexer:

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
        }
    }
}

That means I can treat Foo like an array as follows:

var f1 = new Foo();
SomeObject someObj = f1[1];

In your case the documentation has this:

public Room this[
    Phase phase
] { get; }

What that means is that FamilyInstance has an indexer which takes a Phase instance and returns a Room. So you would use it like this:

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

Or, since it is a named indexer, you can do this:

famInst.FromRoom[somePhase];

The other one is a plain property and documented like this:

public Room FromRoom { get; }

You can only access that property but you cannot set it like this:

Room room = famInst.FromRoom;

So basically, and indexer allows you to treat a class like an array even though the class is not an array. In this case FamilyInstance can be treated like an array. There are many types in .NET which have indexers, for example, String has an indexer like this:

public char this[int index] { get; }

It takes an it and returns a char at that index or it throws an exception if the index is outside the range.

CodingYoshi
  • 25,467
  • 4
  • 62
  • 64
  • so what you are saying is that I was wrong assuming that there are two (2) ways to access the `FromRoom` property. There is only one that requires me to input a Phase as an index argument like so: `famInst.FromRoom[phase]`. Why is there two of them listed in the docs? It confused me. – konrad Dec 27 '16 at 20:09
  • no you are right. One of them is a property which returns `Room`. The other is an indexer which takes 'Phase' and returns a `Room`. In my code, I showed you how to access the one that takes a `Phase` – CodingYoshi Dec 27 '16 at 20:11
  • any ideas on how I can call the other one? Because it only allows me to call the indexer one. I will be happy to award you the answer if you can answer that. – konrad Dec 27 '16 at 20:28
  • I dont understand what you mean, here are the 2 ways: 1. `Room room = famInst.FromRoom;` 2. `Room room = famInst.FromRoom[somePhase];` – CodingYoshi Dec 27 '16 at 20:36
  • if you read my question it states that when I call `famInst.FromRoom` it returns a `ReflectedIndexer` the only method that works is when I actually use the second method with the Phase input. Why would it return a reflected indexer? What is that? – konrad Dec 27 '16 at 20:49
  • It looks like there is no good answer to it. http://stackoverflow.com/questions/39001482/revitapi-ironpython-toroom-returns-indexer-object Apologies. – konrad Dec 27 '16 at 20:55
  • @konrad do me a favor: can you please put the cursor on FamilyInstance and go to the definition (F12) and see if both the indexer and property are there? – CodingYoshi Dec 27 '16 at 21:00
  • @konrad I am not sure what version of the API you are using but I just added a reference to the 2017 NuGet package for RevitAPI and it seems they have fixed the issue. Now there are two: `FromRoom` and `get_FromRoom` (this one is the indexer). – CodingYoshi Dec 27 '16 at 21:34
  • It works in visual studio but it just won't work for me in the IronPython console that I am using it for (Dynamo). I can live with it. For a moment I thought that the interpreter was confusing the two properties and that there was a way to explicitly tell it which one to use. – konrad Dec 27 '16 at 23:28