获取从TypeScript中的特定类型派生的类型的属性的属性值?

获取从TypeScript中的特定类型派生的类型的属性的属性值?,typescript,reflection,Typescript,Reflection,我有一个抽象类Section,它将用于表示文档的一个部分,该部分可以是有效的,也可以是无效的。此类节也可以具有嵌入式节。如果节包含无效的内部节,则该节无效 我创建了类ASection1和ASection2,目的是将它们用作MySection的内部部分,通过performValidation()调用验证过程 如何获取从类MySection派生的类型的属性。我需要关于抽象类反射逻辑的帮助,如下所述 abstract class Section { constructor(public na

我有一个抽象类
Section
,它将用于表示文档的一个部分,该部分可以是有效的,也可以是无效的。此类节也可以具有嵌入式节。如果节包含无效的内部节,则该节无效

我创建了类
ASection1
ASection2
,目的是将它们用作
MySection
的内部部分,通过
performValidation()
调用验证过程

如何获取从类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();  

谢谢。

如果您每次都有两个或多个相同类型和相同含义的属性,请使用数组代替每个属性。我向每个子部分添加子部分数组。在验证中,我检查每个子部分,如果任何子部分验证失败循环返回false,如果没有子部分验证失败,验证将通过验证自身继续进行

abstract class Section {
    protected subsections: Array<Section> = [];

    constructor(public name: string) {
    }

    performValidation(): boolean {

        let result: boolean = true;

        for (var i = 0; i < this.subsections.length; i++) {
            if (!this.subsections[i].Validate()) {
                return;
            }
        }

        return this.Validate();
    }

    public addSubsection(section: Section) {
        this.subsections.push(section);
    }

    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"); 
   }

   prop1: number;

   Validate(): boolean {
      return this.prop1 > 100;
   }

}

//run

let mySect = new MySection();
mySect.prop1 = 101;
mySect.addSubsection(new ASection1());
mySect.addSubsection(new ASection2());
mySect.performValidation(); 
抽象类部分{
受保护的子部分:数组=[];
构造函数(公共名称:字符串){
}
performValidation():布尔值{
让结果:布尔值=真;
对于(var i=0;i100;
}
}
//跑
让mySect=newmySection();
myspect.prop1=101;
mySelect.addSubsection(新的ASection1());
mySelect.addSubsection(新的ASection2());
mySelect.performValidation();

如果每次都有两个或多个相同类型和相同含义的属性请使用数组代替每个属性。我向每个子部分添加子部分数组。在验证中,我检查每个子部分,如果任何子部分验证失败循环返回false,如果没有子部分验证失败,验证将通过验证自身继续进行

abstract class Section {
    protected subsections: Array<Section> = [];

    constructor(public name: string) {
    }

    performValidation(): boolean {

        let result: boolean = true;

        for (var i = 0; i < this.subsections.length; i++) {
            if (!this.subsections[i].Validate()) {
                return;
            }
        }

        return this.Validate();
    }

    public addSubsection(section: Section) {
        this.subsections.push(section);
    }

    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"); 
   }

   prop1: number;

   Validate(): boolean {
      return this.prop1 > 100;
   }

}

//run

let mySect = new MySection();
mySect.prop1 = 101;
mySect.addSubsection(new ASection1());
mySect.addSubsection(new ASection2());
mySect.performValidation(); 
抽象类部分{
受保护的子部分:数组=[];
构造函数(公共名称:字符串){
}
performValidation():布尔值{
让结果:布尔值=真;
对于(var i=0;i100;
}
}
//跑
让mySect=newmySection();
myspect.prop1=101;
mySelect.addSubsection(新的ASection1());
mySelect.addSubsection(新的ASection2());
mySelect.performValidation();

为什么不将节存储在地图数组中,而不是存储未知的单个属性?为什么不将节存储在地图数组中,而不是存储未知的单个属性?