getting container element type with reflection

C

colin

Hi,
Im using reflection and I have a general handler for a container type
object where I have the System.Type info of the container itself
but I need to find out the type of the element contained in it.

The full name of the Type info does usualy include the type,
but is there a way of getting this without parsing the fullname ?

the container type maybe a Byte[] or a class <maybe with a template> wich
implements ICollection.

I need to fill the container from a file.

the element can be anything from a Byte to a packed binary struct
or another class, or more awkwardlky a struct wich cant be bit copied.

the size is either stored before the contents or is derived perviously.

Colin =^.^=
 
M

Marc Gravell

For arrays, Type.GetElementType() should work; however, for
collections you may need to check for IList<T> and then infer the T.

Marc
 
C

colin

Marc Gravell said:
For arrays, Type.GetElementType() should work; however, for collections
you may need to check for IList<T> and then infer the T.

thanks, thats helpfull for arrays,

im not sure what you mean by infer the T
I dont know what T is so im not sure how I look for IList<T> without knowing
before hand
unless I just try all the known posibilities of T?
this would only work for primitive types though, not for classes.

Colin =^.^=
 
M

Marc Gravell

Like so:

Type unknownType = typeof(List<string>); // pretend we
don't know this ;-p
foreach (Type intType in unknownType.GetInterfaces()) {
if (intType.IsGenericType &&
intType.GetGenericTypeDefinition()
== typeof(IList<>)) {
Type elementType =
intType.GetGenericArguments()[0]; // ****
Trace.WriteLine("Implements IList<T> for T = " +
elementType.FullName);
}
}

At the line marked "****" we have the element-type.

Marc
 
C

colin

Marc Gravell said:
Like so:

Type unknownType = typeof(List<string>); // pretend we don't
know this ;-p
foreach (Type intType in unknownType.GetInterfaces()) {
if (intType.IsGenericType &&
intType.GetGenericTypeDefinition()
== typeof(IList<>)) {
Type elementType = intType.GetGenericArguments()[0]; //
****
Trace.WriteLine("Implements IList<T> for T = " +
elementType.FullName);
}
}

At the line marked "****" we have the element-type.

Marc

cool thanks, I didnt understand it at first,
but i tried it and although I had only implmented the non generic
collection in my wrapper, just adding the IList<T>
and letting it implement it with defualt members
it finds the type of 'T' :D

Colin =^.^=
 
M

Marc Gravell

I didnt understand it at first
Sorry, I was in a hurry to run for a train, so didn't have time to add
comments... let me know if you want a run-through...
 
C

colin

Marc Gravell said:
Sorry, I was in a hurry to run for a train, so didn't have time to add
comments... let me know if you want a run-through...

no thats fine thanks very much :)

I just hadnt got to grips with stuff that says generic in it till now.

I seem to have jumped in at the deep end using reflection on my first c# app
lol.

Colin =^.^=
 
M

Marc Gravell

Just remember: reflection is a valid option, but it should not be
your /default/ option - quite the opposite: it is there for when you
cannot *possibly* know anything about the types at compile time. MOst
of the time there are often far more graceful ways to do the same
thing. Just don't get into the habit of using it ;-p

Happy coding,

Marc
 
C

colin

Marc Gravell said:
Just remember: reflection is a valid option, but it should not be
your /default/ option - quite the opposite: it is there for when you
cannot *possibly* know anything about the types at compile time. MOst
of the time there are often far more graceful ways to do the same
thing. Just don't get into the habit of using it ;-p

yeah, I knew I had a lot of structures to import from an old
file format, and they were big too, and also subject to user modification
c# seemed to offer quite an attractive route, its not that slow either,
has been quite a hard learning curve though.

its also been usefull for dumping the data structures for debug too.
it would be a long chore to write serialise in,out, and ToString
for every structure.

reflection just breezes through it all now.

for my next trick im going to tackle user defined types lol.
although I may try and avoid it and do the user script stuff some other way,
or just avoid it altogether. for now the user defined fields are treated
like
database fields.

Colin =^.^=
 

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