来自Kotlin文档页面:
// public final class Gson {
// ...
// public <T> T fromJson(JsonElement json,
// Class<T> classOfT)
// throws JsonSyntaxException {
// ...
在上面的代码片段中,除了
Class<T>
这个东西以外,我都能理解。我猜它是以下内容的C#等价物:public sealed class Gson
{
public T FromJson<T>(JsonElement json,
System.Type Type)
{
}
}
客户端代码可能会像这样说:
var gson = new Gson();
var customer = gson.FromJson<Customer>(json, typeof(Customer));
但我并不能确定,因为方法中的泛型类型参数 T
已经足以表达意思,System.Type
参数似乎有点多余。
另外,在同一页中的同一位置,下面这段代码中的 class.java
是什么意思?
inline fun <reified T: Any> Gson.fromJson(json):
T = this.fromJson(json, T::class.java)
我假设Java中的类
Class
类似于System.Type
,因此如果你想说typeof(Customer)
,你可以使用Customer.class
。这正确吗?
class.java
是什么?
class.java
是一个有良好文档记录的 Kotlin 兼容构造。 - chrylis -cautiouslyoptimistic-Class<T>
参数的需求问题。class.java
是什么意思?我已经更新了问题,加入了另一个 Kotlin 代码片段。 - Water Cooler v2Class
类相当于CLR中的System.Type
。它并没有回答整个问题。 - Water Cooler v2