Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Doesn't IKVM.net support generics (type parameters)?

I statically recompiled a Java lib which used generics a lot, like Collection<?>, but the emitted .NET dll only uses Collection, not with type parameters. How come?

like image 200
Carl Hörberg Avatar asked Sep 25 '09 12:09

Carl Hörberg


1 Answers

Java generics are dealt by the Java compiler and are converted to non-generic version at compile time. This is different from .NET where the CLR has first class support for type parameters. At the bytecode level, ArrayList<T> will just be a simple ArrayList.

To quote 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.

like image 129
mmx Avatar answered Sep 28 '22 05:09

mmx