获取java.util.Map参数的通用类型 [英] Get generic type for java.util.Map parameter

查看:406
本文介绍了获取java.util.Map参数的通用类型的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

public Object[] convertTo(Map source, Object[] destination) {
    ...
}

是否可以通过反射找出我的Map参数的通用类型(键/值)?

Is there a possibility to figure out the generic types (key / value) of my Map parameter via Reflection?

推荐答案

给出Map<Key,Value>,在运行时无法弄清KeyValue.这是由于类型擦除(另请参见

Given a Map<Key,Value>, it isn't possible to figure out Key and Value at runtime. This is due to type erasure (also, see Wikipedia).

但是,可以检查地图中包含的每个对象(键或值),然后调用它们的

It is, however, possible to examine each object (key or value) contained in the map, and call their getClass() method. This will tell you the runtime type of that object. Note that this still won't tell you anything about the compile-type types Key and Value.

这篇关于获取java.util.Map参数的通用类型的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆