If I have retrieved a type using typeof()
, can I somehow use that to build List
, Array
, and IEnumerable
types for the single type?
I want to do this specifically so that I can do comparisons to find out if an unknown type is either a single version of itself or an IEnumerable
or some other type of collection.
Here's a code example. Explanation is below the code.
public void Example()
{
Type k = typeof(Kitten);
Type m = typeof(Whatever); //Could even be a List<Whatever>, or Whatever[], or IEnumerable<Whatever>
Compare(k, m);
}
public void Compare(Type someType, Type otherType)
{
Type ListOfSomeType = ?????;
Type IEnumreableOfSomeType = ?????;
if(otherType == someType)
//Its a Kitten
elseif(otherType == ListOfSomeType)
//Its a List<Kitten>
elseif(otherType == IEnumreableOfSomeType)
//Its an IEnumerable<Kitten>
}
The point is that the Compare
function gets Type
arguments containing the types. It has no idea what they actually are. But it needs to be able to find out if they are the same type, or if the second one is some kind of collection of the first one.
I entered question marks (?????) for the imaginary code I would like to use to construct the collection types.
Is this even possible?
Aucun commentaire:
Enregistrer un commentaire