Revit API Forum
Welcome to Autodesk’s Revit API Forums. Share your knowledge, ask questions, and explore popular Revit API topics.
cancel
Showing results for 
Show  only  | Search instead for 
Did you mean: 

Why do Revit Set classes only implement IEnumerable instead of IEnumerable<T>?

6 REPLIES 6
Reply
Message 1 of 7
JOfford_13
791 Views, 6 Replies

Why do Revit Set classes only implement IEnumerable instead of IEnumerable<T>?

The Revit API contains lots of "Set" classes such as ParameterSet, ProjectLocationSet, PlanTopologySet, etc that only go as far as implementing IEnumerable.  Is there any particular reason they don't implement IEnumerable<T> where T is underlying Revit class type?

 

I find myself always casting the set to the underlying type or explicitly defining the class when using for-each loops.

element.Parameters.Cast<Parameter>().Where(p => criteria);
foreach (Parameter p in element.Parameters) { // .. }

The fact that the Sets only implement IEnumerable makes me a bit nervous doing this.  Is the above considered acceptable/safe code in Revit?  Are there times when a Set like ParameterSet actually returns Objects that aren't Parameters?  If so, please clarify when I might expect such an event.

 

6 REPLIES 6
Message 2 of 7
JimJia
in reply to: JOfford_13

Hi JOfford_13,

 

ParemeterSet is a set that contains parameters. All objects in it are paremeters.

So there is no need to cast object to Parameter, just use foreach as follows:

foreach (Parameter p in element.Parameters)
{
    // ..
}

Jim Jia
Autodesk Forge Evangelist
https://forge.autodesk.com
Developer Technical Services
Autodesk Developer Network
Email: Jim.Jia@autodesk.com
Message 3 of 7
JOfford_13
in reply to: JimJia

Is there a reason then why the ParameterSet doesn't implement IEnumerable<Parameter>?  Why are we forced to cast it from an Object to a Parameter every time?

Message 4 of 7
JimJia
in reply to: JOfford_13

Hi JOfford_13,

 

I am communicating with engineer team, will keep you updated once I get any information.


Jim Jia
Autodesk Forge Evangelist
https://forge.autodesk.com
Developer Technical Services
Autodesk Developer Network
Email: Jim.Jia@autodesk.com
Message 5 of 7
r.terry
in reply to: JimJia

Any further information on this yet?

Message 6 of 7
JOfford_13
in reply to: JOfford_13

any updates on this?

Message 7 of 7

Until we get word back, there are a few workarounds you can do instead of casting. I usually use OfType, which gives you the typed IEnumerable that you can then use LINQ function notation on without having to cast each member of the set. You can also use the LINQ query notation. For example,  (in vb.net)

Dim test As IEnumerable = {1, 2, 3, 4, 5}
Console.WriteLine(String.Join(" ", (From n In test Select n * 2)))

Prints "2 4 6 8 10" successfully. For more on using query notation look here.

 

Can't find what you're looking for? Ask the community or share your knowledge.

Post to forums  

Forma Design Contest


Rail Community