It seems that i have found a strange bug in .NET-2.0/unity's .NET-2.0.
public class Test
{
public interface ICovariant<out T>
{
T GetT();
}
public class Covariant<T>: ICovariant<T>
{
public T GetT()
{
throw new NotImplementedException();
}
}
public interface IFoo
{
}
public class Bar : IFoo
{
}
public class CovariantHolder
{
public CovariantHolder(ICovariant<IFoo> covariant)
{
}
}
public class CovariantBar: Covariant<Bar>
{
}
public static void Main(string[] args)
{
ICovariant<IFoo> foo = new Covariant<IFoo>();
ICovariant<IFoo> foobar = new Covariant<Bar>();
ICovariant<IFoo> bar = new CovariantBar();
CovariantBar barbar = new CovariantBar();
new CovariantHolder(foo); // works
new CovariantHolder(foobar); // works
new CovariantHolder(bar); // works
new CovariantHolder(barbar); // works
Console.WriteLine(typeof(ICovariant<IFoo>).IsAssignableFrom(typeof(Covariant<IFoo>))); // true;
Console.WriteLine(typeof(ICovariant<IFoo>).IsAssignableFrom(typeof(Covariant<Bar>))); // true;
Console.WriteLine(typeof(ICovariant<IFoo>).IsAssignableFrom(typeof(CovariantBar))); // false! what?;
typeof(CovariantHolder).GetConstructors()[0].Invoke(new object[] {foo}); //works
typeof(CovariantHolder).GetConstructors()[0].Invoke(new object[] {foobar}); //fails! WHAAT?
typeof(CovariantHolder).GetConstructors()[0].Invoke(new object[] {bar}); //fails!
typeof(CovariantHolder).GetConstructors()[0].Invoke(new object[] {barbar}); //fails!
}
}
AFAIK, unity uses mono 2.0.50727.1433
Is there any way around it? Can I instatniate an object through reflection using covariant argument?
Aucun commentaire:
Enregistrer un commentaire