Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. We have updated the language to the Editor Terms based on feedback from our employees and community. Learn more.
    Dismiss Notice
  3. Join us on November 16th, 2023, between 1 pm and 9 pm CET for Ask the Experts Online on Discord and on Unity Discussions.
    Dismiss Notice

Generic Variance Issues

Discussion in 'Editor & General Support' started by Nakedsense, Jul 2, 2015.

  1. Nakedsense

    Nakedsense

    Joined:
    Dec 13, 2012
    Posts:
    2
    Hello there,
    I've noticed that the mono runtime in unity doesn't support a feature that's available since .Net 2.0: generic variance.

    Long story short, below code works fine in .Net 2.0, 3.5 and 4.5 (which I've tested). But throws an ArrayTypeMismatchException at `consumers.Add(new BaseConsumer());`.

    Code (CSharp):
    1. using System;
    2. using System.Collections.Generic;
    3.  
    4. interface IBase
    5. {
    6.     int x {get;}
    7. }
    8.  
    9. class BaseClass : IBase
    10. {
    11.     public int x {get; set;}
    12. }
    13.  
    14. interface IDerived : IBase
    15. {
    16.     int y {get;}
    17. }
    18.  
    19. class DerivedClass : BaseClass, IDerived
    20. {
    21.     public int y {get; set;}
    22. }
    23.  
    24. interface IConsumer<in T> where T: class, IBase
    25. {
    26.     void Feed(T arg);
    27. }
    28.  
    29. class BaseConsumer : IConsumer<IBase>
    30. {
    31.     public void Feed(IBase arg)
    32.     {
    33.         Console.WriteLine(arg.x);
    34.     }
    35. }
    36.  
    37. class DerivedConsumer : IConsumer<IDerived>
    38. {
    39.     public void Feed(IDerived arg)
    40.     {
    41.         Console.WriteLine(arg.y);    
    42.     }
    43. }
    44.  
    45. public class Program
    46. {
    47.     public static void Main()
    48.     {
    49.         List<IConsumer<IDerived>> consumers = new List<IConsumer<IDerived>>();
    50.         consumers.Add(new BaseConsumer());
    51.         consumers.Add(new DerivedConsumer());
    52.  
    53.         DerivedClass d = new DerivedClass() { x = 3, y = 5};
    54.  
    55.         foreach (IConsumer<IDerived> consumer in consumers)
    56.             consumer.Feed(d);
    57.     }
    58. }
    Is there a fix or workaround available? Or is it going to be fixed, ever? I've seen questions about this in SO and Unity Answers from 2013, but I haven't been able to find any answer.

    Thank you very much.
     
  2. Immanuel-Scholz

    Immanuel-Scholz

    Joined:
    Jun 8, 2013
    Posts:
    221
    Have you tried compiling the code using a different compiler (e.g. cl.exe) and then importing the DLL into your project?

    If its a problem with the mono compiler generating wrong checks, then this might help.
     
  3. Nakedsense

    Nakedsense

    Joined:
    Dec 13, 2012
    Posts:
    2
    Thank you for your reply, yes I've tried using it from a DLL. I guess since the runtime is problematic, compilation makes no difference :(