C# component with lists of different custom classes

Hello guys,
I am creating a component and at the same slot I want to be able to plug a list of different objects, but the checks fail. Precisely:

if (DA.GetDataList(0, List<XXXXX> provided_as_obj)) {
    var obj = provided_as_obj[0];
    if (obj is Object1) 
      // do something
    else if (obj is Object2)
       // do sth else

Object1 and Object2 are custom made classes.

  1. Do I need to implement something in order to be able to use the obj is Object1 check? My checks always fail.
  2. How should I declare provided_as_obj and what should I put in place of XXXXX? I used List<object> but as I said the checks later fail.

What confuses me is that when Object1 is string and Object2 is int, for example, the checks work.

I got some inspiration from here: multiple type input/output from custom components - Grasshopper and c# - Type Checking: typeof, GetType, or is? - Stack Overflow


Try like this

var provided_as_goo = new List<GH_ObjectWrapper>();
if (DA.GetDataList(0, ref provided_as_goo)) {
    foreach(var goo in provided_as_goo) {
        var obj = goo.ScriptVariable();
        if (obj is Object1 obj1) 
           // do something
        else if (obj is Object2 obj2)
           // do sth else

Worked like a charm! Thanks a lot!