Kotlin TypeToken“未解决的参考”错误

第5,6和7行中出现错误“未解决的响应引用”。您能帮我理解这个问题吗?

public class GenericCall { public fun genecicCall(functionToBeCalled:String, responseType:String, requestType:String, vararg args:String){ var request = Class.forName(requestType).newInstance() var response = Class.forName(responseType).newInstance() var finalType = object : TypeToken<GenericResponse<Class<response>>>(){}.getType() var creditresponse: GenericResponse<response>? = AjaxHelper.ajax(Constants.Ajax.ENDPOINT_CREDIT, Constants.Ajax.REQUEST_POST, request, finalType, null) return ResponseProcessing.processResponse(creditresponse as Any) as response? } } } 

泛型类型参数不能是一个表达式,它应该是另一个类型,比如一个类,可能带有其他泛型类型参数。 因此,如果Foo是静态的(由编译器推断的)类型的response那么不要使用Class<response> (第5行),否则应写入Class<Foo> 。 在你的情况下,除了它是Any的一个实例之外,你可能不知道任何response 。 所以你可以使用Class<Any>Class<*> ,无论你喜欢什么。 与GenericResponse类似(第6行)。

由于相同的原因,表达式也不能出现在as运算符的右侧(第7行):该运算符将左侧转换为右侧指定的类型 。 此外,你的函数似乎没有返回任何东西,所以从它返回一个有用的值将无法正常工作。