I have method look like
doSomething( Class<T> valueType)
i call pass value like
Map.class, Long.class
as parameter into above method. If i want to use generic. how should i pass the value into above method? doSomething(Class<Map>); doesnt work
Map.classreturns an object of typeClass<Map>,Long.classreturns an object of typeClass<Long>.So to pass
Map.classyour T needs to me Map.If you want your T to be a generic, say
ArrayList<String>, then that’s not possible.The reason is that generics in Java use type erasure, which means that the generic type is only there at compile time, but it’s erased at run time. Thus there doesn’t exists something like
ArrayList<String>.class.Java generics are quite different that C# generics in this sense.