Type Casting With Generics

R

rajivpopat

I've been reading a discussion thread at
http://groups.google.com/group/micr...ges.csharp/browse_frm/thread/119f8362a9f5ff52
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.
 
N

Nicholas Paldino [.NET/C# MVP]

rajivpopat,

This code will work, but it will create a copy of the collection which
could get out of synch.

If this is a concern, what I would do is create a class which acts as a
wrapper to the IList implementation. It would take the IList in it's
constructor, and the type would take a generic type parameter for the
IList<T> implementation. You then just forward the calls to the underlying
IList implementation.

Hope this helps.
 
R

rajivpopat

Hi Nicholas,

You're 100% correct in saying that the copy of the collection could get
out of sync. And now that i think about it - i realize how ... crappy
my code was :) becuase it just made a one time snap shot copy of the
list and then doesn't do anything! Thanks for pointing this out.

So, what i understand from your suggestion is that i have just one
non-generic list which is actually maintained. The Class that inherits
off IList<T> just implements methods and ensures that any updates on
the generic list are actually passed back to the non-generic list by
invoking it's native methods. Here's the code i wrote up based on your
idea:
(Again, this code 'seems to work' but PLEASE do let me know what you
think about this... just want to be sure i am on the right learning
track).

/// <summary>
/// Converts All Non Generic Lists of n-Hibernate to Generics
/// </summary>
/// <typeparam name="T"></typeparam>
public class NSList<T> : IList<T>
{
IList _NonGenericItems = null;

public NSList(T GenericItems)
{

}
public NSList()
{

}
public NSList(IList NonGenericItems)
{
_NonGenericItems = NonGenericItems;
// If the 'wrapping has occurred successfully then
technically this
// crappy code is not required!
/*
for (int ncounter = 0; ncounter < NonGenericItems.Count;
ncounter++)
{
this.Add((T)NonGenericItems[ncounter]);
}
*/

}


#region IList<T> Members

public int IndexOf(T item)
{
return _NonGenericItems.IndexOf(item);
}

public void Insert(int index, T item)
{
_NonGenericItems.Insert(index, item);
}

public void RemoveAt(int index)
{
_NonGenericItems.RemoveAt(index);
}

public T this[int index]
{
get
{
return (T) _NonGenericItems[index];
}
set
{
_NonGenericItems[index] = value;
}
}

#endregion

#region ICollection<T> Members

public void Add(T item)
{
_NonGenericItems.Add(item);
}

public void Clear()
{
_NonGenericItems.Clear();
}

public bool Contains(T item)
{
return _NonGenericItems.Contains(item);
}

public void CopyTo(T[] array, int arrayIndex)
{
_NonGenericItems.CopyTo(array, arrayIndex);
}

public int Count
{
get { return _NonGenericItems.Count; }
}

public bool IsReadOnly
{
get { return _NonGenericItems.IsReadOnly; }
}

public bool Remove(T item)
{
// This Remove always returned a void so i am always going
to
// return a true?
_NonGenericItems.Remove(item);
return true;
}

#endregion

#region IEnumerable<T> Members

public IEnumerator<T> GetEnumerator()
{
// I am not sure if this is Correct.
return (IEnumerator < T >)
_NonGenericItems.GetEnumerator();
}

#endregion

#region IEnumerable Members

IEnumerator IEnumerable.GetEnumerator()
{
// Not sure if this is correct
return (IEnumerator<T>)_NonGenericItems.GetEnumerator();
}

#endregion
}
 

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

Similar Threads


Top