fork download
  1. using System;
  2. using System.Collections.Generic;
  3. using System.IO;
  4.  
  5. public class Test
  6. {
  7. public static void Main()
  8. {
  9. List<INonGenericWrapper> wrappers = new List<INonGenericWrapper>();
  10. wrappers.Add(new Wrapper<object>());
  11. wrappers.Add(new Wrapper<string>());
  12. wrappers.Add(new Wrapper<int>());
  13. var visitor = new SerializationVisitor();//Create the operation you need to apply
  14. foreach (var wrapper in wrappers)
  15. {
  16. wrapper.Accept(visitor);
  17. }
  18. }
  19. }
  20.  
  21. public class Matrix<T>
  22. {
  23. public T Obj { get; set; }
  24. }
  25.  
  26. public interface INonGenericWrapper
  27. {
  28. void Wrap();
  29. void Accept(IVisitor visitor);
  30. }
  31.  
  32. public class Wrapper<T> : INonGenericWrapper
  33. {
  34. public Matrix<T> Matrix { get; private set; }
  35.  
  36. public void Wrap()
  37. {
  38. //Your domain specific method
  39. }
  40.  
  41. public void Accept(IVisitor visitor)
  42. {
  43. visitor.Visit(this);
  44. }
  45. }
  46.  
  47. public interface IVisitor
  48. {
  49. void Visit<T>(T element);
  50. }
  51.  
  52. public class SerializationVisitor : IVisitor
  53. {
  54. public void Visit<T>(T element)
  55. {
  56. new Serializer<T>().Serialize(element);
  57. }
  58. }
  59.  
  60. public class Serializer<T>
  61. {
  62. public Stream Serialize(T objectToSerialize)
  63. {
  64. Console.WriteLine("Serializing {0}", objectToSerialize);
  65. //Your serialization logic here
  66. return null;
  67. }
  68. }
Success #stdin #stdout 0.03s 34768KB
stdin
Standard input is empty
stdout
Serializing Wrapper`1[System.Object]
Serializing Wrapper`1[System.String]
Serializing Wrapper`1[System.Int32]