Kotlin:如何访问与扩展同名的字段?

Kotlin: How to access a field with the same name as an extension?

我有以下字段扩展

import com.google.gson.annotations.SerializedName

val Enum<*>.serializedName: String get() = javaClass.getField(name).getAnnotation(SerializedName::class.java).value

现在,如果我的枚举具有与 serializedName 扩展相同的字段名称,我如何访问扩展字段。 我目前的做法是转换为 Enum

enum class MyEnum {
    FIELD_1,
    FIELD_2
    ;
    val serializedName = (this as Enum<*>).serializedName 
}

你不能。它在官方 kotline 文档中描述 https://kotlinlang.org/docs/reference/extensions.html

If a class has a member function, and an extension function is defined which has the same receiver type, the same name, and is applicable to given arguments, the member always wins. For example: