How to find the type T of a generic.

N

Narshe

I have a custom collection class that looks like this

public class CustomCollection<T> : ICollection<T>, IList<T>,
IEnumerable<T>, IDisposable, ICloneable, IComparer<T>
{....}

I need to know if the type T has implemented a certain interface
ICustomClass.

I can't do

public class CustomCollection<T> where T : ICustomClass,
IColltion<T>......

because then I would need to implement ICustomClass. I just want this
collection class to only have objects in it that implement
ICustomClass.

I figured in the constructor I could check if T is of type
ICustomClass, but I can't figure this out.

I could check when the objects are actually added, but I'd like the
check to happen right when it's created. Compile time would be best.

Any suggestions?
 
M

Martin Robins

You have to provide at least an implementation of the ICustomClass interface
so that you can check against it ...

public interface ICustomClass {
}

You can then check at compile time that your generic collection is being
used with objects that implement your custom interface

public class CustomCollection<T> : ICollection<T?, IList<T>, IEnumerable<T>,
IDisposable, ICloneable, IComparer<T>
where T : ICustomClass {

...

}

Define you actual implementation when you are ready

public class CustomObject : ICustomClass {

...

}

And the compiler with validate for you

public CustomCollection<CustomObject> {
}


There is no other way around it if I understand your requirement correctly!
 
M

Marc Gravell

Simple : just add the following to the end of your interface list:

where T : ICustomClass

This insists that you can only create CustomCollection<T> instances for that
meets this contract. This also means that inside the class anything typed as
T will know that it meets the ICustomClass interface, so you will get the
intellisense etc without having to do any casting.

You could also do this at runtime with a static constuctor, but that's
harder...

Marc
 
M

Marc Gravell

For completeness (going back to your original question), to get the Type of
T, just use typeof(T).

If you wanted to code it the other way (static constructor), you could get
the Type and then you can check anything about the Type that you want using
reflection; to be honest, the only time I can think of when this would be
handy is if you were happy to accept *any 1* of a set of interfaces /
base-classes, or need a particular (non-default) constructor - i.e. where
the "where" or "new" clauses don't help. As an example (based on yours, but
trimmed purely so that it compiles):

public interface ICustomClass { }
public class CustomCollection<T> where T : ICustomClass {
static CustomCollection() {
Type type = typeof(T);
if (type.GetConstructor(new Type[] { typeof(string) }) ==
null)
throw new NotSupportedException(type.Name + " does not
meet the required ctor(string) signature for use in CustomCollection<T>");
}
}

Marc
 
I

Ignacio Machin \( .NET/ C# MVP \)

Hi,

I can think of two options:

create an instance of T
T t = new T();

ICustomClass ic = t as ICustomClass ;
if ( ic == null ) //does not implement it

The problem with the above is that you need to create an instance of T

Second variant is using reflection
Type t = typeof(T)

you can use Type.FindInterfaces or Type.GetInterface



I would go for the second method

cheers,
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top