c# - Extract derived elements from a list of base elements -
I have an interface list that stores different objects taken from the interface. I would like to write a function where the caller specifies the type to extract I have tried to:
list & lt; IParts & gt; part of the list;
...
Public list & lt; IPart & gt; Receive (type type) {return.PartList.OfType & lt; PartType & gt; (). Cast & lt; IPart & gt; (). ToList (); }
But it does not like an passed type. any suggestion?
If you have to support different types of runtime (instead of compiling time with generic):
Public List & lt; IPart & gt; Receive (type type) {if (! Typeof (IPart) .IsAssignableFrom (PartType)} {New ArgumentOutOfRangeException ("PartType", "Received from IPart"); } Return it. The delist. Type (i = & gt; i! = Null & amp; amp; amp; amp; amp;; & amp; section; i.GetType ()). ToList (); }
Comments
Post a Comment