Is it possible to retrieve the reflect-metadata from an instance of a class? The documentation provides examples that suggest it should be achievable, but when I attempt to do so, I receive undefined
as a result. Strangely enough, when I request the metadata directly from the class or its methods, I am able to access the data.
Here is an example script to illustrate:
import 'reflect-metadata'
const metadataKey = 'some-key'
@Reflect.metadata(metadataKey, 'hello class')
class C {
@Reflect.metadata(metadataKey, 'hello method')
get name(): string {
return 'text'
}
}
let obj = new C()
let classInstanceMetadata = Reflect.getMetadata(metadataKey, obj)
console.log(classInstanceMetadata) // undefined
let classMetadata = Reflect.getMetadata(metadataKey, C)
console.log(classMetadata) // hello class
let methodMetadata = Reflect.getMetadata(metadataKey, obj, 'name')
console.log(methodMetadata) // hello method
The objective here is to obtain some sort of data in the classInstanceMetadata
variable that allows for association with the class type.