IList<Type> problem

I’m having trouble creating a IList for an ElementMultiClassFilter. I’ve tried the following:

from System import Type

type_list = List[Type]()

It throws an invalid cast exception and I am stuck. Does any of you know how it should be done?

The exception is:

System.InvalidCastException: Error in IEnumeratorOfTWrapper.Current. Could not cast: System.Type in IronPython.Runtime.Types.PythonType ---> System.InvalidCastException: Unable to cast object of type 'IronPython.Runtime.Types.PythonType' to type 'System.Type'.
 at IronPython.Runtime.IEnumeratorOfTWrapper`1.get_Current()
 --- End of inner exception stack trace ---
 at Microsoft.Scripting.Interpreter.NewInstruction.Run(InterpretedFrame frame)
 at Microsoft.Scripting.Interpreter.Interpreter.Run(InterpretedFrame frame)
 at Microsoft.Scripting.Interpreter.LightLambda.Run4[T0,T1,T2,T3,TRet](T0 arg0, T1 arg1, T2 arg2, T3 arg3)
 at System.Dynamic.UpdateDelegates.UpdateAndExecute3[T0,T1,T2,TRet](CallSite site, T0 arg0, T1 arg1, T2 arg2)
 at Microsoft.Scripting.Interpreter.DynamicInstruction`4.Run(InterpretedFrame frame)
 at Microsoft.Scripting.Interpreter.Interpreter.Run(InterpretedFrame frame)
 at Microsoft.Scripting.Interpreter.LightLambda.Run2[T0,T1,TRet](T0 arg0, T1 arg1)
 at IronPython.Compiler.PythonScriptCode.RunWorker(CodeContext ctx)
 at PyRevitLabs.PyRevit.Runtime.IronPythonEngine.Execute(ScriptRuntime& runtime)

Hi @thumDer
That might help

1 Like

This works fine for me

from System import Type
from System.Collections.Generic import List
m = List[Type]()
print(m)

Try getting the type with clr.GetClrType(Type) and pass to the List[]
List[clr.GetClrType(Type)]()

Thanks for the help , I’ve mangaged to figure it out.
The problem was not List[Type]() but List[Type]([DB.Wall, DB.Floor, etc...]) which works fine for other kind of System.List objects, but not for this.
Instead I had to do the following to make it work:

class_list = List[Type]()
class_list.Add(DB.Wall)
class_list.Add(DB.Floor)
...

this is less elegant but can be prettified as:

classes = [DB.Wall, DB.Floor, ...]
class_list = List[Type]()
for i in classes: class_list.Add(i)
2 Likes

Oh yes. Then you need to grab the Type instead.

import clr

l =  List[Type]([clr.GetClrType(t) for t in [DB.Wall, DB.Floor]])
1 Like

That makes sense, thank you!

1 Like