规则可观察的代表

时间:2017-12-03 21:55:02

标签: delegates kotlin observable

在某些情况下是否有可能规避可观察的代表?

用例:

val ls: ArrayList<SomeType> by Delegates.observable(arrayListOf()) {
    _, _, new ->
    if (someCondition) {
        usesList(new)
        // I want to reset ls to arrayListOf(), but without the invocation of the observable delegate.
    }
}

1 个答案:

答案 0 :(得分:0)

当然......你必须在你的课程路径中有kotlin反射。而且我不建议使用它。 :)

val delegatedProperties = D::class.java.getDeclaredField("\$\$delegatedProperties").get(d) as Array<KProperty<*>>
(delegatedProperties.find { it.name == "ls" } as KMutableProperty<*>).setter.call(arrayListOf<Any>())