Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- public class NonGenericCollection:CollectionBase
- {
- public void Add(TestClass a)
- {
- List.Add(a);
- }
- }
- public class ConvertTest
- {
- public static List<TestClass> ConvertToGenericClass( NonGenericCollection collection)
- {
- // Ask for help here.
- }
- }
- public static List<TestClass> ConvertToGenericClass(NonGenericCollection collection)
- {
- return collection.Cast<TestClass>().AsList();
- }
- public static List<TestClass> ConvertToGenericClass(NonGenericCollection collection)
- {
- return collection.OfType<TestClass>().AsList();
- }
- public class EnumerableGenericizer<T> : IEnumerable<T>
- {
- public IEnumerable Target { get; set; }
- public EnumerableGenericizer(IEnumerable target)
- {
- Target = target;
- }
- IEnumerator IEnumerable.GetEnumerator()
- {
- return GetEnumerator();
- }
- public IEnumerator<T> GetEnumerator()
- {
- foreach(T item in Target)
- {
- yield return item;
- }
- }
- }
- IEnumerable<MyClass> genericized =
- new EnumerableGenericizer<MyClass>(nonGenericCollection);
- public class ConvertTest
- {
- public static List<TestClass> ConvertToGenericClass( NonGenericCollection collection) throws I
- {
- List<TestClass> newList = new ArrayList<TestClass>
- for (Object object : collection){
- if(object instanceof TestClass){
- newList.add(object)
- } else {
- throw new IllegalArgumentException();
- }
- }
- return newList;
- }
- }
Add Comment
Please, Sign In to add comment