在 C# 中,我们使用 DataAnnotation 在属性上添加元属性。我需要 TypeScript 中的此功能用于 ldap 模型类。装饰者应该设置 LDAP 目录内部使用的 LDAP 属性
export class LdapUser {
@ldapAttribute('sn')
sureName: string;
}
export function ldapAttribute(attributeName: string): (target: any, propertyKey: string) => void {
return function (target: any, propertyKey: string) {
Reflect.defineMetadata("ldapAttribute", attributeName, target, propertyKey);
}
}
但是要获取ldapAttribute
装饰器值,我需要将对象和属性名称作为原始字符串传递,如下所示:
let user = new LdapUser();
let sureNameAttribute = Reflect.getMetadata("ldapAttribute", user, "sureName"); // sn
它可以工作,但这似乎是不好的做法,因为当sureName
属性被重命名LdapUser
而不将其应用于Reflect.getMetadata()
调用时,它会导致运行时而不是编译器错误。而且智能感知也缺失了。所以我正在寻找这样的解决方案:
let sureNameAttribute = Reflect.getMetadata("ldapAttribute", user.sureName);
这里的问题是我需要某种反射来划分user.SureName
属性名称(这里sureName
)和类对象(这里user
)。我已经在 C# 中使用反射做过类似的事情,但不知道如何在 TS 中做到这一点。
解决方法
它不如在 C# 中使用反射那么好,但比只使用纯字符串要好:
export function getLdapAttribute<T>(instance: T, attributeName: keyof T) : string {
let value : string = Reflect.getMetadata("ldapAttribute", instance, attributeName);
return value;
}
用法
let attributeValue = getLdapAttribute(user, "sureName"); // cn
可悲的是,我们这里没有智能感知。但是如果属性名称不存在,至少我们会得到一个编译器错误。