471,319 Members | 1,226 Online
Bytes | Software Development & Data Engineering Community
Post +

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 471,319 software developers and data experts.

How to find the type T of a generic.

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?

Nov 29 '05 #1
5 8571
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!

"Narshe" <na****@gmail.com> wrote in message
news:11**********************@f14g2000cwb.googlegr oups.com...
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?

Nov 29 '05 #2
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

"Narshe" <na****@gmail.com> wrote in message
news:11**********************@f14g2000cwb.googlegr oups.com...
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?

Nov 29 '05 #3
Thanks.

I didn't know the where clause could go after the interface list.

Nov 29 '05 #4
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

"Narshe" <na****@gmail.com> wrote in message
news:11*********************@f14g2000cwb.googlegro ups.com...
Thanks.

I didn't know the where clause could go after the interface list.

Nov 29 '05 #5
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,

--
Ignacio Machin,
ignacio.machin AT dot.state.fl.us
Florida Department Of Transportation

"Narshe" <na****@gmail.com> wrote in message
news:11**********************@f14g2000cwb.googlegr oups.com...
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?

Nov 30 '05 #6

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

21 posts views Thread by Walter L. Preuninger II | last post: by
5 posts views Thread by Joerg Battermann | last post: by
5 posts views Thread by chellappa | last post: by
16 posts views Thread by vizzz | last post: by
reply views Thread by rosydwin | last post: by

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.