【发布时间】:2015-07-07 07:13:47
【问题描述】:
我尝试做这样的事情:
Poperty<Integer> prop;
JsonObject jsonObject = (JsonObject) parser.parse(string);
if(jsonObject.get("type").equals("Integer")){
prop = gson.fromJson(jsonObject, Property<Integer>.class);
}
else if{...}
问题出在:Property<Integer>.class。
是否有可能以某种方式传递通用.class?
【问题讨论】:
-
类对象表示运行时编译的源文件。因此,这些对象不能采用通用参数。它们代表类型为 T 的类。
Property.class将为您提供匹配的类对象,但我不知道 Gson 是如何工作的。 -
是的,我知道,代码只是为了阐明我想要实现的目标。非泛型 Property.class 作为参数会起作用,但这不是我想要的。