Ответ 1
Если вам нужно только получить геттеры/сеттеры, вам нужно будет сделать что-то вроде:
class Test {
...
public static getGetters(): string[] {
return Object.keys(this.prototype).filter(name => {
return typeof Object.getOwnPropertyDescriptor(this.prototype, name)["get"] === "function"
});
}
public static getSetters(): string[] {
return Object.keys(this.prototype).filter(name => {
return typeof Object.getOwnPropertyDescriptor(this.prototype, name)["set"] === "function"
});
}
}
Test.getGetters(); // ["RowsCount", "RowsCount2"]
Test.getSetters(); // ["RowsCount", "RowsCount2"]
Вы можете поместить статические методы в базовый класс, а затем, когда вы его расширите, подкласс также будет иметь эти статические методы:
class Base {
public static getGetters(): string[] {
return Object.keys(this.prototype).filter(name => {
return typeof Object.getOwnPropertyDescriptor(this.prototype, name)["get"] === "function"
});
}
public static getSetters(): string[] {
return Object.keys(this.prototype).filter(name => {
return typeof Object.getOwnPropertyDescriptor(this.prototype, name)["set"] === "function"
});
}
}
class Test extends Base {
...
}
Test.getGetters(); // work the same
Если вы хотите, чтобы эти методы были методами экземпляра, вы можете сделать это:
class Base {
public getGetters(): string[] {
return Object.keys(this.constructor.prototype).filter(name => {
return typeof Object.getOwnPropertyDescriptor(this.constructor.prototype, name)["get"] === "function"
});
}
public getSetters(): string[] {
return Object.keys(this.constructor.prototype).filter(name => {
return typeof Object.getOwnPropertyDescriptor(this.constructor.prototype, name)["set"] === "function"
});
}
}
Изменение заключается в том, что вместо this.prototype
вы используете this.constructor.prototype
.
Тогда вы просто:
let a = new Test();
a.getGetters(); // ["RowsCount", "RowsCount2"]