我正在尝试从类的实例中检索反射元数据。文档上的示例表明它应该是可能的,但我得到了undefined
. 但是,如果我从类本身请求元数据,我会取回数据,与方法相同。
例如,这是完整的示例脚本:
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
我的目标是取回一些数据classInstanceMetadata
,让我将其与类类型相关联。