gpt4 book ai didi

c# - IKVM.net 不支持泛型(类型参数)吗?

转载 作者:塔克拉玛干 更新时间:2023-11-03 04:26:40 25 4
gpt4 key购买 nike

我静态地重新编译了一个经常使用泛型的 Java 库,比如 Collection<?> ,但发出的 .NET dll 仅使用 Collection ,不带类型参数。怎么会?

最佳答案

Java 泛型由 Java 编译器处理,并在编译时转换为非泛型版本。这与 .NET 不同,在 .NET 中,CLR 具有对类型参数的一流支持。在字节码级别,ArrayList<T>将只是一个简单的 ArrayList .

引用Java docs :

Generics are implemented by the Java compiler as a front-end conversion called erasure, which is the process of translating or rewriting code that uses generics into non-generic code (that is, maps the new syntax to the current JVM specification). In other words, this conversion erases all generic type information; all information between angle brackets is erased. For example, LinkedList<Integer> will become LinkedList. Uses of other type variables are replaced by the upper bound of the type variable (for example, Object), and when the resulting code is not type correct, a cast to the appropriate type is inserted.

关于c# - IKVM.net 不支持泛型(类型参数)吗?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/1477038/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com