NOTE: When I originally asked this question I included IEnumerable. After some responses, I realized it was obvuscating the question because I was mixing interfaces and classes. I've removed it. Now I'm only asking about these three class types:
- a type that represents a single thing
- a type that represents a list of those things
- a type that represents an array of those things
Before responding, please make absolutely sure you understand what I'm asking. I am NOT asking about:
- types that derive from another type
- types that are assignable from another type
- types that implement some interface
Edited question:
If I have retrieved a type using typeof()
, can I somehow use that to check for types that represent lists or arrays of the retrieved type?
I want to do this specifically so that I can do comparisons to find out if an unknown type is either some other specific type or a list or array of that specific type.
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[]
Compare(k, m);
}
public void Compare(Type someType, Type otherType)
{
Type ListOfSomeType = ?????;
Type ArrayOfSomeType = ??????;
if(otherType == someType)
//otherType is a Kitten
elseif(otherType == ListOfSomeType)
//otherType is a List<Kitten>
elseif(otherType == ArrayOfSomeType)
//otherType is a 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 a TYPE OF list or array storing objects of the first kind of type.
I entered question marks (?????) for the imaginary code I would like to use to construct the collection types.
How can I do this? This question is not hypothetical.
Aucun commentaire:
Enregistrer un commentaire