私は抽象クラスSection
を持っています。これは、有効であるか無効である可能性のある文書のセクションを表すために使用されます。このようなセクションには、埋め込みセクションもあります。無効な内部セクションが含まれているセクションは有効ではありません。TypeScriptの特定の型から派生した型のプロパティのプロパティ値を取得しますか?
私は、検証プロセスがperformValidation()
によって呼び出さされているMySection
の内側のセクションとしてそれらを使用する目的のためのクラスASection1
とASection2
を作成しました。
MySectionクラスから派生した型のプロパティを取得する方法を教えてください。私は以下のように、抽象クラスリフレクションロジックの助けが必要です。
abstract class Section {
constructor(public name: string) {
}
performValidation(): boolean {
let result: boolean = true;
//HELP IS NEEDED HERE!!!
//get all properties of this instance
//whose type inherit from "Section"
//and execute Validate() on each of them
//one at a time, if any of them returns
//false, return false.
return this.Validate();
}
abstract Validate(): boolean;
}
class ASection1 extends Section {
constructor() {
super("Section example 1");
}
Validate(): boolean {
//validation logic goes here
}
}
class ASection2 extends Section {
constructor() {
super("Section example 2");
}
Validate(): boolean {
//validation logic goes here
}
}
class MySection extends Section {
constructor() {
super("My Section");
}
subsection1: ASection1;
subsection2: ASection2;
prop1: number;
Validate(): boolean {
return this.prop1 > 100;
}
}
//run
let mySect = new MySection();
mySect.prop1 = 101;
mySect.subsection1 = new ASection1();
mySect.subsection2 = new ASection2();
mySect.performValidation();
ありがとうございます。
不明な個々のプロパティではなく、マップの配列にセクションを保存するだけの理由はありますか? –