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

JOfford_13
Advocate
Advocate

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

JOfford_13
Advocate
Advocate

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.

 

Reply
929 Views
6 Replies
Replies (6)

JimJia
Alumni
Alumni

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
0 Likes

JOfford_13
Advocate
Advocate

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?

0 Likes

JimJia
Alumni
Alumni

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
0 Likes

r.terry
Explorer
Explorer

Any further information on this yet?

0 Likes

JOfford_13
Advocate
Advocate

any updates on this?

0 Likes

john_dalessandro
Contributor
Contributor

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.

 

0 Likes