1

I've got a list of objects = { obj1,obj2,obj3 }

Every class of the objects is inherited from the same interface

interface IObjects
class Obj1:IObjects
class Obj2:IObjects
class Obj3:IObjects

And I want to find object of Obj1 class for examp. How to do it?

5 Answers5

4

You can do so by:

var listOfObject1s = objects.Where(o => o is Obj1).ToList();
ntziolis
  • 10,091
  • 1
  • 34
  • 50
1
if(obj.GetType() == typeof(Obj1))
{
    // obj is an Obj1!
}
Moo-Juice
  • 38,257
  • 10
  • 78
  • 128
1

Iterate through the list and check for

item is Obj1
Michel Keijzers
  • 15,025
  • 28
  • 93
  • 119
1

Linq method:

IObjects[] objList = new IObjects[] { obj1,obj2,obj3 };
obj1 o1 = objList.Where(o => o.GetType() = typeof(Obj1)).First();
D Stanley
  • 149,601
  • 11
  • 178
  • 240
1

Call GetType() on your object. see here. Hope this helps

ldgorman
  • 1,553
  • 1
  • 14
  • 39