当我注意到这个奇怪的部分时,我正在阅读Roslyn源代码:
// Implicit casts are not emitted. As a result verifier may operate on a different
// types from the types of operands when performing stack merges in coalesce/ternary.
// Such differences are in general irrelevant since merging rules work the same way
// for base and derived types.
//
// Situation becomes more complicated with delegates, arrays and interfaces since they
// allow implicit casts from types that do not derive from them. In such cases
// we may need to introduce static casts in the code to prod the verifier to the
// right direction
Run Code Online (Sandbox Code Playgroud)
我很好奇这将会发生什么.我特别关心接口何时允许来自非派生类型的隐式转换.但是,对数组/代理的解释也很有趣.
小智 2
这似乎是泛型中协方差的一个经典示例:您可以将接口与更泛型的类一起使用,而不是派生类。
所以他们
“允许从不派生自它们的类型进行隐式转换”
因为在这里您有从具有基类型的接口到具有派生类型的接口的隐式转换,因此从不从它们派生的类型(具有基类型的接口)(具有派生类型的接口)。
在我的示例中,您可以看到一个协变接口,它计算派生程度较高的形状的面积,因为它是派生程度较低的形状,因此您实际上有一个转换,例如,一个维度丢失了......
public class Basic
{
public double dim1;
}
public class Derived : Basic
{
public double dim2;
}
public interface IFactory<in T>
{
double Area(T shape);
}
class BasicFactory : IFactory<Basic>
{
public double Area(Basic shape)
{
return shape.dim1 * shape.dim1;
}
}
class DerivedFactory : IFactory<Derived>
{
public double Area(Derived shape)
{
return shape.dim1 * shape.dim2;
}
}
class Program
{
double Area(IFactory<Derived> factory, Derived shape)
{
return factory.Area(shape);
}
static void Main(string[] args)
{
IFactory<Basic> notDerived = new BasicFactory(); // from not derived type
Derived shape = new Derived() { dim1 = 10, dim2 = 20 };
double area = new Program().Area(notDerived,shape); // cast! dimension loss
Console.WriteLine(area); // 100 = 10*10
IFactory<Derived> derived = new DerivedFactory(); //from derived type
area = new Program().Area(derived, shape); // no cast, now
Console.WriteLine(area); // 200 = 10*20
Console.ReadKey();
}
}
Run Code Online (Sandbox Code Playgroud)