Guest User

Untitled

a guest
Jun 23rd, 2018
100
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.52 KB | None | 0 0
  1. public class NonGenericCollection:CollectionBase
  2. {
  3. public void Add(TestClass a)
  4. {
  5. List.Add(a);
  6. }
  7. }
  8.  
  9. public class ConvertTest
  10. {
  11. public static List<TestClass> ConvertToGenericClass( NonGenericCollection collection)
  12. {
  13. // Ask for help here.
  14. }
  15. }
  16.  
  17. public static List<TestClass> ConvertToGenericClass(NonGenericCollection collection)
  18. {
  19. return collection.Cast<TestClass>().AsList();
  20. }
  21.  
  22. public static List<TestClass> ConvertToGenericClass(NonGenericCollection collection)
  23. {
  24. return collection.OfType<TestClass>().AsList();
  25. }
  26.  
  27. public class EnumerableGenericizer<T> : IEnumerable<T>
  28. {
  29. public IEnumerable Target { get; set; }
  30.  
  31. public EnumerableGenericizer(IEnumerable target)
  32. {
  33. Target = target;
  34. }
  35.  
  36. IEnumerator IEnumerable.GetEnumerator()
  37. {
  38. return GetEnumerator();
  39. }
  40.  
  41. public IEnumerator<T> GetEnumerator()
  42. {
  43. foreach(T item in Target)
  44. {
  45. yield return item;
  46. }
  47. }
  48. }
  49.  
  50. IEnumerable<MyClass> genericized =
  51. new EnumerableGenericizer<MyClass>(nonGenericCollection);
  52.  
  53. public class ConvertTest
  54. {
  55. public static List<TestClass> ConvertToGenericClass( NonGenericCollection collection) throws I
  56. {
  57. List<TestClass> newList = new ArrayList<TestClass>
  58. for (Object object : collection){
  59. if(object instanceof TestClass){
  60. newList.add(object)
  61. } else {
  62. throw new IllegalArgumentException();
  63. }
  64. }
  65. return newList;
  66. }
  67. }
Add Comment
Please, Sign In to add comment