fork(2) download
  1. using System;
  2. using System.Linq;
  3.  
  4. /* Interfaces */
  5. public interface IArgumentClass
  6. {
  7. void IArgumentClassMethod();
  8. }
  9. public interface ISpecialArgumentClass
  10. {
  11. void ISpecialArgumentClassMethod();
  12. }
  13. public interface IContainerClass
  14. {
  15. void IContainerClassClassMethod();
  16. }
  17.  
  18. /* Argument types */
  19. public class ArgumentClass0 : IArgumentClass
  20. {
  21. public void IArgumentClassMethod(){}
  22. }
  23. public class SpecialArgumentClass0 : IArgumentClass, ISpecialArgumentClass
  24. {
  25. public void IArgumentClassMethod(){}
  26. public void ISpecialArgumentClassMethod(){}
  27. }
  28. public class SpecialArgumentClass1 : IArgumentClass, ISpecialArgumentClass
  29. {
  30. public void IArgumentClassMethod() { }
  31. public void ISpecialArgumentClassMethod() { }
  32. }
  33.  
  34. /* Container types */
  35. public class GenericContainer<T> : IContainerClass
  36. where T : IArgumentClass, new()
  37. {
  38. public T t = new T();
  39. public void IContainerClassClassMethod() { }
  40. }
  41. public class NonGenericContainer : IContainerClass
  42. {
  43. public void IContainerClassClassMethod(){}
  44. }
  45.  
  46. /* main program */
  47. public class Test
  48. {
  49. public static void Main()
  50. {
  51. // Instantiate
  52. IContainerClass[] containers =
  53. {
  54. new GenericContainer<ArgumentClass0>(),
  55. new GenericContainer<SpecialArgumentClass0>(),
  56. new GenericContainer<SpecialArgumentClass1>(),
  57. new NonGenericContainer()
  58. };
  59.  
  60. // We want to call IContainerClassClassMethod methods on all instances:
  61. foreach (IContainerClass container in containers)
  62. container.IContainerClassClassMethod();
  63.  
  64. // We want to call ISpecialArgumentClassMethod on instances where it's possible:
  65. foreach (IContainerClass container in containers)
  66. {
  67. if (container.GetType().IsGenericType && container.GetType().GetGenericTypeDefinition() == typeof(GenericContainer<>))
  68. {
  69. foreach (Type typeArgument in container.GetType().GetGenericArguments())
  70. {
  71. if (typeArgument.GetInterfaces().Contains(typeof(ISpecialArgumentClass)))
  72. {
  73. // Next line is a compilation error. How can I get a similar result?
  74. GenericContainer<ISpecialArgumentClass> mySpecializedClassWithSpecialArgument = container as GenericContainer<ISpecialArgumentClass>;
  75. mySpecializedClassWithSpecialArgument.t.ISpecialArgumentClassMethod();
  76. }
  77. }
  78. }
  79. }
  80. }
  81. }
Compilation error #stdin compilation error #stdout 0s 0KB
stdin
Standard input is empty
compilation info
prog.cs(74,25): error CS0311: The type `ISpecialArgumentClass' cannot be used as type parameter `T' in the generic type or method `GenericContainer<T>'. There is no implicit reference conversion from `ISpecialArgumentClass' to `IArgumentClass'
prog.cs(35,14): (Location of the symbol related to previous error)
prog.cs(74,25): error CS0310: The type `ISpecialArgumentClass' must have a public parameterless constructor in order to use it as parameter `T' in the generic type or method `GenericContainer<T>'
prog.cs(9,18): (Location of the symbol related to previous error)
prog.cs(74,118): error CS0311: The type `ISpecialArgumentClass' cannot be used as type parameter `T' in the generic type or method `GenericContainer<T>'. There is no implicit reference conversion from `ISpecialArgumentClass' to `IArgumentClass'
prog.cs(35,14): (Location of the symbol related to previous error)
prog.cs(74,118): error CS0310: The type `ISpecialArgumentClass' must have a public parameterless constructor in order to use it as parameter `T' in the generic type or method `GenericContainer<T>'
prog.cs(9,18): (Location of the symbol related to previous error)
Compilation failed: 4 error(s), 0 warnings
stdout
Standard output is empty