I have a data class called report which is essentially a list of lists of variables - so nested data classes.
data class report(
var list1 : List1,
var list2 : List2
) : Parcelable
data class List1 (
var a: Boolean = false,
var b: Boolean = false,
) : Parcelable
data class List2 (
var c : Boolean = false,
var d : Boolean = false,
) : Parcelable
Using Kotlin and reflection I have a for loop which iterates over the top level data class and prints the name of the variable successfully. I'd like to iterate over each nested class and print the variables names but I can't figure out how to do that without explicitly referencing the nested class by name. The first for loop prints the correct data but the second for loop just prints "length".
for (prop in report::class.memberProperties) {
Log.d("DATACLASS", "${prop.name}")
for (subProp in prop::class.memberProperties){
Log.d("DATACLASS", "${subProp.name}")
}
}
In the second loop I feel like "prop" is not being expanded properly as a reference to the nested class. I tried something like "report.prop.name" or "report.${prop.name})" without success. I also tried assigning report.prop.name to a separate variable. Is this a simple syntax issue I'm missing? I did read other similar questions on Stack but didn't find a solution. Is it something to do perhaps with not having a class instance created at the right moment?
Aucun commentaire:
Enregistrer un commentaire