473,463 Members | 1,532 Online
Bytes | Software Development & Data Engineering Community
Create Post

Home Posts Topics Members FAQ

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 8915
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

21
by: Walter L. Preuninger II | last post by:
I would like to write a generic procedure that will take string or numeric variables. I can not think of a way to make this more clear except to show what I want. int main(void) { int i=7;...
2
by: ESPNSTI | last post by:
Hi, I'm trying to use a generics dictionary with a key class that implements and needs IComparable<>. However when I attempt to use the dictionary, it doesn't appear to use the IComparable<> to...
1
by: Dotnet Gruven | last post by:
I've posted this in the adonet group, however it was suggested I might have better luck here.... ============================================================= I'm trying to use a typed dataset and...
5
by: Joerg Battermann | last post by:
Hello there, I have a custom type defined via Public Class Requirement Public IDNumber As Integer Public Name As String Public Description As String Public VersionPlanAttributes As New _
5
by: chellappa | last post by:
Hi All, How to find the data type of the variable ? is there any libaray function avaiable? Because i want create generic data type of some operation . Thanks All By Chellappa
9
by: mps | last post by:
I want to define a class that has a generic parameter that is itself a generic class. For example, if I have a generic IQueue<Tinterface, and class A wants to make use of a generic class that...
5
by: Random | last post by:
How can I use reflection (or some other method) to find the type of an object that has been passed in to my method under an interface definition? I try to use GetType, but that won't work.
1
by: pekbob1 | last post by:
Hi Everybody I have the error (ObjectDataSource 'ObjectDataSource1' could not find a non-generic method 'UpdatePre' that has parameters: Comp, Job_Number, Request_Date, Customer, Contact_Name, Tel,...
16
by: vizzz | last post by:
Hi there, i need to find an hex pattern like 0x650A1010 in a binary file. i can make a small algorithm that fetch all the file for the match, but this file is huge, and i'm scared about...
0
by: Cirene | last post by:
Using Visual Studio I created a DataSet using the GUI (XSD file). Trying to use a tiered methodology I called the functions from my BLL. Namespace Zzz.BusinessLogicLayer #Region "DAL Access"...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
1
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...
0
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The...

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.