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.
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) { // .. }
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?
Hi JOfford_13,
I am communicating with engineer team, will keep you updated once I get any information.
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.