HarmonyOS ArkTS怎么反射获取类中的公有方法数组?

如题:HarmonyOS ArkTS怎么反射获取类中的公有方法数组?

阅读 474
1 个回答

请参考以下示例:

index.ets

import { MethodListUtil } from './MethodListUtil';

@Entry
@Component
struct WebComponent {
  instance:MethodListUtil = new MethodListUtil()
  build() {
    Column() {
      Button('getMethodList')
        .onClick(() => {
          const methodList = MethodListUtil.getMethods(MethodListUtil) as string[]
          console.log('methodList====='+JSON.stringify(methodList))
        })

    }
  }
}

MethodListUtil.ets

export  class MethodListUtil{
  constructor() {
  }
  webTest(): string {
    console.log('Web test');
    return "Web test";
  }
  webString(): void {
    console.log('Web test toString');
  }
  static  getMethods(classInstance: ESObject): string[] {
    return Object.getOwnPropertyNames(classInstance.prototype).filter(name =>
    typeof classInstance.prototype[name] === 'function' && name !== 'constructor');
  }
}
撰写回答
你尚未登录,登录后可以
  • 和开发者交流问题的细节
  • 关注并接收问题和回答的更新提醒
  • 参与内容的编辑和改进,让解决方法与时俱进