需要在 Unity3D 中显式转换的 IEnumerable 协方差

IEnumerable covariance requiring explicit cast in Unity3D

Unity 的 C# 版本实现了协变,但与我在网上找到的任何示例相比,此代码无法编译

string[] stringArray = new string[0];
IEnumerable<string> stringIEnumerable = new string[0];

// string[] implements IEnumerable<string>,
// and implicitly casts to IEnumerable<object> through covariance
IEnumerable<object> objects1 = stringArray;

// But puzzling enough, one cannot implicitly
// convert IEnumerable<string> to IEnumerable<object> here
IEnumerable<object> objects2 = stringIEnumerable;

完整错误:

error CS0266: Cannot implicitly convert type `System.Collections.Generic.IEnumerable<string>' to `System.Collections.Generic.IEnumerable<object>'. An explicit conversion exists (are you missing a cast?)

是的,我现在明确地转换它,但我需要知道为什么会这样。我知道 Unity 的 C# 版本不是最后一个,但这种行为让我觉得很奇怪。我假设没有中间版本的 C# 可以在没有这些隐式转换的情况下实现协方差

附录:我喜欢干净的代码。我想知道如何使这些隐式转换起作用,因为在我使用它们的上下文中它们感觉非常正确和自然

事实证明,协变是当前版本的 Unity 编译器支持的编译器功能,但实际框架是旧版本,其中通用接口未定义为具有协变类型