I've been reading a discussion thread at
http://groups.google.com/group/micro...9f8362a9f5ff52
regarding typecasting generic collections to classical collections and
vice-a-versa
I faced a similar problem and solved it slightly differently... The
apporach i seems to work but i am sure someone has a better apporach
for solving this problem.
In my case i was using nHibernate which was returning Classic IList
collections and I needed to convert those to Generic driven
collection... so i wrote up a General class that will convert all
ILists to IList<T>... Below is my code:
/// <summary>
/// Converts All Non Generic Lists of n-Hibernate to Generics
/// </summary>
/// <typeparam name="T"></typeparam>
class NSList<T> : IList<T>
{
public NSList(T users)
{
}
public NSList(List NonGenericItems)
{
//TODO: Better ways of doing this same thing? ConvertAll
function of
//dotnet ? (rajiv popat)
for (int ncounter = 0; ncounter < NonGenericItems.Count;
ncounter++)
{
this.Add((T)NonGenericItems[ncounter]);
}
}
}
While this seems to do the job the Ugly for loop concerns me... Would
it be a problem if the collection had more objects in future? Any
better ways of doing it?
Bruce / Lebesgue / Anyone else, When this thread talks about Creating
an interface - does it mean
mean an interface that would do somoething similar to what i do in the
class here?
I've just started playing around with the Beta 2.0 so my questions
might be slightly stupid :) and strangely enough i could not reply to
that thread so i started a new one.
regards,
rajiv.