c# - 为什么这种泛型场景会导致 TypeLoadException?

标签 c# generics clr runtime compiler-bug

这有点啰嗦,所以这里是快速版本:

为什么这会导致运行时 TypeLoadException?(编译器应该阻止我这样做吗?)

interface I
{
    void Foo<T>();
}

class C<T1>
{
    public void Foo<T2>() where T2 : T1 { }
}

class D : C<System.Object>, I { } 

如果您尝试实例化 D,则会发生异常。


更长、更具探索性的版本:

考虑:

interface I
{
    void Foo<T>();
}

class C<T1>
{
    public void Foo<T2>() where T2 : T1 { }
}

class some_other_class { }

class D : C<some_other_class>, I { } // compiler error CS0425

这是非法的,因为 C.Foo() 上的类型限制与 I.Foo() 上的不匹配.它会生成编译器错误 CS0425。

但我认为我可以打破规则:

class D : C<System.Object>, I { } // yep, it compiles

通过使用 Object作为对 T2 的约束,我否定该约束。我可以安全地将任何类型传递给 D.Foo<T>() ,因为一切都源自 Object .

即便如此,我仍然预计会出现编译错误。在 C# 语言 意义上,它违反了“C.Foo() 上的约束必须与 I.Foo() 上的约束相匹配”的规则,我认为编译器会坚持规则。但它确实编译。似乎编译器看到了我在做什么,理解它是安全的,并且视而不见。

我以为我已经成功了,但运行时说没那么快。如果我尝试创建 D 的实例,我得到一个 TypeLoadException:“类型 'D' 上的方法 'C`1.Foo' 试图隐式实现具有较弱类型参数约束的接口(interface)方法。”

但是这个错误在技术上不是错误的吗?不使用 Object对于 C<T1>取消对 C.Foo() 的约束,从而使其等同于 - 不强于 - I.Foo() ?编译器似乎同意,但运行时不同意。

为了证明我的观点,我将其简化为 D不等式:

interface I<T1>
{
    void Foo<T2>() where T2 : T1;
}

class some_other_class { }

class C : I<some_other_class> // compiler error CS0425
{
    public void Foo<T>() { }
}

但是:

class C : I<Object> // compiles
{
    public void Foo<T>() { }
}

对于传递给 Foo<T>() 的任何类型,这都能完美地编译和运行.

为什么?运行时是否存在错误,或者(更有可能)是否存在我没​​有看到的此异常的原因 - 在这种情况下,编译器不应该阻止我吗?

有趣的是,如果通过将约束从类移动到接口(interface)来反转场景...

interface I<T1>
{
    void Foo<T2>() where T2 : T1;
}

class C
{
    public void Foo<T>() { }
}

class some_other_class { }

class D : C, I<some_other_class> { } // compiler error CS0425, as expected

我再次否定约束:

class D : C, I<System.Object> { } // compiles

这次运行正常!

D d := new D();
d.Foo<Int32>();
d.Foo<String>();
d.Foo<Enum>();
d.Foo<IAppDomainSetup>();
d.Foo<InvalidCastException>();

任何事情都会发生,这对我来说非常有意义。 (等式中有或没有 D 都一样)

那么为什么第一种方式会中断?

附录:

我忘了补充一点,TypeLoadException 有一个简单的解决方法:

interface I
{
    void Foo<T>();
}

class C<T1>
{
    public void Foo<T2>() where T2 : T1 { }
}

class D : C<Object>, I 
{
    void I.Foo<T>() 
    {
        Foo<T>();
    }
}

明确实现I.Foo()很好。只有隐式实现会导致 TypeLoadException。现在我可以这样做了:

        I d = new D();
        d.Foo<any_type_i_like>();

但这仍然是一个特例。尝试使用除 System.Object 之外的任何其他内容,这将无法编译。我觉得这样做有点脏,因为我不确定它是否故意以这种方式工作。

最佳答案

这是一个错误 - 请参阅 Implementing Generic Method From Generic Interface Causes TypeLoadExceptionUnverifiable Code with Generic Interface and Generic Method with Type Parameter Constraint .不过,我不清楚这是 C# 错误还是 CLR 错误。

[由 OP 添加:]

这是 Microsoft 在您链接到的第二个线程中所说的(我强调的):

There is a mismatch between the algorithms used by the runtime and the C# compiler to determine if one set of constraints is as strong as another set. This mismatch results in the C# compiler accepting some constructs that the runtime rejects and the result is the TypeLoadException you see. We are investigating to determine if this code is a manifestation of that problem. Regardless, it is certainly not "By Design" that the compiler accepts code like this that results in a runtime exception.

Regards,

Ed Maurer C# Compiler Development Lead

从我加粗的部分来看,我认为他是在说这是一个编译器错误。那是在 2007 年。我想这还不够严重,不足以成为他们修复它的优先事项。

关于c# - 为什么这种泛型场景会导致 TypeLoadException?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6012420/

相关文章:

c# - 在单元测试中向对象断言未经验证的添加属性

c# - 在不可变的构造函数方法中验证构造函数参数是坏主意还是好主意?

c++ - 会有标准类型列表吗?

python-3.x - MyPy 不允许受约束的 TypeVar 协变?使用受约束但协变的键值类型定义通用字典

c# - C++/CLI clr :safe for C# COM Interop 中的包装器 C++

.net - 如何限制特定类可以分配的内存?

c# - 如何使用 C# 从 xml 文件中删除所有空元素?

ios - 如何将 'Any?' 转换为具有灵活通用属性的结构?

.net - CLR 类中的内存泄漏

c# - 将数组传递给非托管代码并在不复制的情况下返回