Determining the Data Type of a Generic
-
I have a class,
public class Collection<T>
and I was wondering if there was a way to determine if the data type used implements an interface. I have used this code.if (typeof(T) == typeof(ICollectable))
variance = 1.2;but it never gets into this if block, regardless if the class I use implements ICollectable. Any ideas?
-
I have a class,
public class Collection<T>
and I was wondering if there was a way to determine if the data type used implements an interface. I have used this code.if (typeof(T) == typeof(ICollectable))
variance = 1.2;but it never gets into this if block, regardless if the class I use implements ICollectable. Any ideas?
Sure... When you call
typeof(T)
, it returns a Type object, which has plenty of useful methods like GetInterface(), FindInterface(), etc. Just don't use IsSubclassOf(), as that won't work for interfaces.Proud to have finally moved to the A-Ark. Which one are you in?
Author of the Guardians Saga (Sci-Fi/Fantasy novels) -
I have a class,
public class Collection<T>
and I was wondering if there was a way to determine if the data type used implements an interface. I have used this code.if (typeof(T) == typeof(ICollectable))
variance = 1.2;but it never gets into this if block, regardless if the class I use implements ICollectable. Any ideas?
I've come up with this, which will work for now. I am unsure of it's efficency though, I assume it uses Reflection
if (typeof(T).GetInterface("ICollectable") != null)
-
I have a class,
public class Collection<T>
and I was wondering if there was a way to determine if the data type used implements an interface. I have used this code.if (typeof(T) == typeof(ICollectable))
variance = 1.2;but it never gets into this if block, regardless if the class I use implements ICollectable. Any ideas?
Reflection is probably the best way to go, but here is another technique which may be faster, though is not as flexible (i.e., you must know in advance if the type is a class or a struct, and it requires the class have a default constructor):
public bool IsClassAnimal<T>()
where T:new()
{
T t = new T();
return t is IAnimal;
}public bool IsStructAnimal<T>()
{
T t = default(T);
if (t == null)
{
throw new InvalidOperationException("The type was a class.");
}
else
{
return t is IAnimal;
}
}Sample interface/struct/class:
public interface IAnimal
{
}
public class Dog : IAnimal
{
}
struct Cat : IAnimal
{
}Sample usage:
if (IsClassAnimal<Dog>())
{
MessageBox.Show("A dog is an animal!");
}
if (IsStructAnimal<Cat>())
{
MessageBox.Show("A cat is an animal!");
}You could simplify that by just passing an instance of the class/struct, but that doesn't seem like what you're after. Also, if your class/struct is large (e.g., with lots of member variables), this could be a very expensive operation; not to mention it could have some weird side-effects. But, hey, thought I'd throw the technique out there anyway in case somebody can make use of it.
-
I have a class,
public class Collection<T>
and I was wondering if there was a way to determine if the data type used implements an interface. I have used this code.if (typeof(T) == typeof(ICollectable))
variance = 1.2;but it never gets into this if block, regardless if the class I use implements ICollectable. Any ideas?