How do I get the type of a generic typed class within the class?
An example:
I build a generic typed collection implementing ICollection< T>. Within I have methods like
public void Add(T item){ ... } public void Add(IEnumerable<T> enumItems){ ... }
How can I ask within the method for the given type T?
The reason for my question is: If object is used as T the collection uses Add(object item) instead of Add(IEnumerable<object> enumItems) even if the parameter is IEnumerable. So in the first case it would add the whole enumerable collection as one object instead of multiple objects of the enumerable collection.
So i need something like
if (T is object) { // Check for IEnumerable }
but of course that cannot work in C#. Suggestions?
Thank you very much!
Michael
Personally, I would side step the issue by renaming the
IEnumerable<T>method toAddRange. This avoids such issues, and is consistent with existing APIs such asList<T>.AddRange.It also keeps things clean when the
Tyou want to add implementsIEnumerable<T>(rare, I’ll admit).