473,503 Members | 11,508 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

getting container element type with reflection

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 templatewich
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 =^.^=
Nov 19 '07 #1
8 2080
For arrays, Type.GetElementType() should work; however, for
collections you may need to check for IList<Tand then infer the T.

Marc
Nov 19 '07 #2
"Marc Gravell" <ma**********@gmail.comwrote in message
news:ec**************@TK2MSFTNGP04.phx.gbl...
For arrays, Type.GetElementType() should work; however, for collections
you may need to check for IList<Tand 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<Twithout 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 =^.^=
Nov 19 '07 #3
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<Tfor T = " +
elementType.FullName);
}
}

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

Marc
Nov 19 '07 #4

"Marc Gravell" <ma**********@gmail.comwrote in message
news:eT*************@TK2MSFTNGP03.phx.gbl...
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<Tfor 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 =^.^=
Nov 19 '07 #5
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...
Nov 19 '07 #6
"Marc Gravell" <ma**********@gmail.comwrote in message
news:d9**********************************@i37g2000 hsd.googlegroups.com...
>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...
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 =^.^=
Nov 19 '07 #7
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
Nov 19 '07 #8
"Marc Gravell" <ma**********@gmail.comwrote in message
news:aa**********************************@p69g2000 hsa.googlegroups.com...
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 =^.^=
Nov 20 '07 #9

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

1
2201
by: Wolfgang Lipp | last post by:
my question is: do we need container elements for repeating elements in data-centric xml documents? or is it for some reason very advisable to introduce containers in xml documents even where not...
6
2421
by: Jon Slaughter | last post by:
Is there any way to get the class type from a pointer? I'm working on a class that acts as a set: I have two classes, one called FSet and the other called Element. FSet inherets Element and...
3
1443
by: Peteris Krumins | last post by:
Hello! I would like to ask how to search for a container element which matches some criteria. Here is what I mean. Suppose I have a structure: struct my_struct { my_struct(const std::string...
10
5633
by: Josselin | last post by:
(Mac 10.4.6 , Firefox/1.5.0.3 Mozilla/5.0 (Macintosh; U; PPC Mac OS X Mach-O; fr; rv:1.8.0.3) Gecko/20060426 ) I am trying to setup a Reusable Image Cache in JS ( as per...
10
6247
by: VK | last post by:
Richard Cornford wrote: > The html element is the outermost element in an (x)HTML document. > All other elements are its descendants, with its direct children being > the head and body elements. ...
4
4015
by: Daniel Marques | last post by:
I would like to know how to get the address of a container of the STL without using iterators. I have a class with three STL lists and a index which points to an element of any ot the other lists,...
16
6042
by: Juha Nieminen | last post by:
I'm actually not sure about this one: Does the standard guarantee that if there's at least one element in the data container, then "--container.end()" will work and give an iterator to the last...
6
15821
by: rn5a | last post by:
What's the difference between <%# DataBinder.Eval(Container.DataItem,"LastName") %> & <%# Container.DataItem("LastName") %> Thanks
5
1223
by: h3xx | last post by:
Sorry, this isn't specifically related to PHP, but to an odd bug I came across when testing some code inside IE7. The problem is that IE7 freezes, but only when the following conditions are met:...
0
7098
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
7296
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
7364
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...
0
7470
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
5604
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,...
1
5026
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
3186
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...
0
3174
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
0
405
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence...

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.