Re: getting container element type with reflection

Tech-Archive recommends: Fix windows errors by optimizing your registry




"Marc Gravell" <marc.gravell@xxxxxxxxx> wrote in message
news:eTKcuUsKIHA.280@xxxxxxxxxxxxxxxxxxxxxxx
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 =^.^=


.