有一个例子
export class BIDS {
static readonly ADDRESS: IBID = { ID: '849541B3-1C2B-4D68-97B5-BC2212FB5B59', UID: 'address' };
static readonly ORDER_FORM: IBID = { ID: '73713334-DE9E-43A1-8F7C-D664A9CA37C6', UID: 'orderform' };
static readonly ORDER_LINE: IBID = { ID: '21A0018B-5BBC-4A18-B439-D5133685F793', UID: 'orderline' };
static getIDbyUID(uid: string): string {
return ????
}
}
export interface IBID {
ID: string;
UID: string;
}
我想知道,是否可以在不创建属性数组的情况下编写getIDByUID?
static getIDbyUID(uid: string): string {
return BIDS.<static_properties>.find(e => e.UID == uid).ID;
}
我的意思是如何以编程方式收集这些属性,而无需人工枚举([ BIDS.ADDRESS, BIDS.ORDER_FORM ... ]
)
答案 0 :(得分:1)
尝试这样
Class clazz = AopUtils.getTargetClass(proxy);
Arrays.stream(clazz.getDeclaredFields()).forEach(field -> System.out.println(field.getName()));
export class BIDS { static readonly ADDRESS: IBID = { ID: '849541B3-1C2B-4D68-97B5-BC2212FB5B59', UID: 'address' }; static readonly ORDER_FORM: IBID = { ID: '73713334-DE9E-43A1-8F7C-D664A9CA37C6', UID: 'orderform' }; static readonly ORDER_LINE: IBID = { ID: '21A0018B-5BBC-4A18-B439-D5133685F793', UID: 'orderline' }; static getIDbyUID(uid: string): string { let props = Object.keys(BIDS); let filterDatas = props.map(p => BIDS[p]); return (filterDatas.find(e => e.UID == uid) || {}).ID } }
将返回Object.keys(BIDS);
所有BIDS属性包括所有方法,但是可以的话,您将获得["getIDbyUID", "ADDRESS", "ORDER_FORM", "ORDER_LINE"]