我们可以访问kotlin中任何财产的PropertyMetaData吗?

我们可以访问kotlin中任何财产的PropertyMetaData吗?,kotlin,Kotlin,kotlin中是否有方法从属性外部访问属性元数据?更具体地说,来自代表团? 寻找这样的东西: 编辑(现在是更合适的示例) class Obj(变量名称:String=”“) 分类栏{ val prop1:Obj by Inject.Inject()//显然,您需要为属性调用委托对象上的一些初始化代码。有几种解决方案: 1) 如bashor所述,使用属性引用表达式获取属性名称并将其传递给inject: val prop1: Obj by Inject.inject(Bar::prop1.name)

kotlin中是否有方法从属性外部访问
属性元数据
?更具体地说,来自代表团? 寻找这样的东西:

编辑(现在是更合适的示例)

class Obj(变量名称:String=”“)
分类栏{

val prop1:Obj by Inject.Inject()//显然,您需要为属性调用委托对象上的一些初始化代码。有几种解决方案:

1) 如bashor所述,使用属性引用表达式获取属性名称并将其传递给
inject

val prop1: Obj by Inject.inject(Bar::prop1.name)
val prop2: Obj by Inject.inject(Bar::prop2.name)
这有点冗长而且容易出错,尽管很明确

2) 在第一次访问属性时执行初始化逻辑。因此
Inject
本身成为属性委托,维护已注册属性的映射。但是,语义上有一点变化可能不适用于您的用例:在中注册属性至少需要一个
get
反>注入

class Bar {
    val prop1: Obj by Inject
    val prop2: Obj by Inject
}

object Inject {
    val injected = hashMapOf<String, ReadOnlyProperty<Any, Obj>>()

    fun get(obj: Any, metadata: PropertyMetadata): Obj {
        // getOrPut computes and stores the value for the key if it's not present in the map
        val property = injected.getOrPut(metadata.name) {
            Delegates.lazy {
                Obj(metadata.name)
            }
        }

        return property[obj, metadata]
    }
}

谢谢你的全面回答!!实际上上一个解决方案正是我想要的。因为在以前的解决方案中,我必须至少调用一次属性才能进行“注入”在val injected中注册。我将考虑潜在的更改:)因为最近的预发布propertyDelegated似乎不再被调用。是否发生了重命名或删除?事实上,它已被暂时禁用,直到1.0。但计划在Kotlin的未来版本中重新设计。至于您的问题,我想我的第一个问题是解决方案应该适合您。有没有办法手动模拟PropertyLegate方法调用?不幸的是,前两个解决方案与我的用例不匹配。我需要抓住一点,在初始化委托时,至少处理使用委托的类中的所有属性(通过反射?)。请详细说明为什么第一个解决方案不适用于您?您可以在
inject
对象中声明
fun inject(p:KProperty):inject
,该对象将执行初始化操作并返回对象本身,并像我在上面提供的示例中一样使用它。
 public final fun <T : Actor, R : Actor> O2dInjectionClosure<T>.replaceBy(replacer : (replaced : T) -> R) : O2dInjectionClosure<R> {
    val originalInitializer = this.initializer
    return O2dInjectionClosure { propName ->

        ... some transferring of the previous O2dInjectionClosure to this one
        new
    }
}
val prop1: Obj by Inject.inject(Bar::prop1.name)
val prop2: Obj by Inject.inject(Bar::prop2.name)
class Bar {
    val prop1: Obj by Inject
    val prop2: Obj by Inject
}

object Inject {
    val injected = hashMapOf<String, ReadOnlyProperty<Any, Obj>>()

    fun get(obj: Any, metadata: PropertyMetadata): Obj {
        // getOrPut computes and stores the value for the key if it's not present in the map
        val property = injected.getOrPut(metadata.name) {
            Delegates.lazy {
                Obj(metadata.name)
            }
        }

        return property[obj, metadata]
    }
}
class Bar {
    val prop1: Obj by Inject
    val prop2: Obj by Inject
}

object Inject {
    val injected = hashMapOf<String, ReadOnlyProperty<Any, Obj>>()

    // Experimental method recognized and called by Kotlin on delegated property initialization
    fun propertyDelegated(metadata: PropertyMetadata) {
        injected[metadata.name] = Delegates.lazy {
            Obj(metadata.name)
        }
    }

    fun get(obj: Any, metadata: PropertyMetadata): Obj {
        return injected[metadata.name]!!.get(obj, metadata)
    }
}