我可以使用创建自定义装饰器reflect-metadata
,它工作正常。
问题是,我不知道如何获取所有实例装饰器。
import 'reflect-metadata';
console.clear();
function readTypes() {
const decorator: MethodDecorator = (target, propertyKey, description) => {
const args = Reflect.getMetadata(
'design:paramtypes',
target,
propertyKey
).map(c => c.name);
const ret = Reflect.getMetadata('design:returntype', target, propertyKey);
console.log(`Arguments type: ${args.join(', ')}.`);
console.log(`Return type: ${ret.name}.`);
};
return decorator;
}
class Foo {}
class Bar {
@readTypes()
public fn(a: number, b: string, c: Foo): boolean {
return true;
}
}
const barInstance = new Bar();
我想@readTypes
从barInstance
. 我该怎么做?
请参阅工作示例: https ://stackblitz.com/edit/decorators-metadata-example-nakg4c