使用 TypeLite 生成具有 DataMember 名称的 C# 类
Using TypeLite to generate C# classes with DataMember name
我目前正在使用 TypeLite 从一组 C# 类 构建 .d.ts 接口文件。我 运行 遇到一个问题,其中某些 类 具有 DataMember 的属性,其中给定值与 属性 名称不同。在这种情况下,我希望 TypeLite 使用 DataMember 属性而不是 属性 名称 - 不幸的是,我在文档中找不到任何地方说这是可能的。
有什么想法吗?
code 仅检查内置 [TsProperty]
属性以重命名属性:
var attribute = memberInfo.GetCustomAttribute<TsPropertyAttribute>(false);
if (attribute != null) {
if (!string.IsNullOrEmpty(attribute.Name)) {
this.Name = attribute.Name;
}
this.IsOptional = attribute.IsOptional;
}
您可以简单地对其进行修补,使其也包括 [DataMember]
属性:
var dataMemberAttribute = memberInfo.GetCustomAttribute<System.Runtime.Serialization.DataMemberAttribute>(false);
if (dataMemberAttribute!= null) {
if (!string.IsNullOrEmpty(dataMemberAttribute.Name)) {
this.Name = dataMemberAttribute.Name;
}
this.IsOptional = !dataMemberAttribute.IsRequired;
}
也许您可以提交包含该修复程序的拉取请求。确保添加测试并考虑将两个属性应用于 属性.
的情况
为了保持一致性,您还必须打补丁以支持 [DataContract]
属性才能重命名 类.
我目前正在使用 TypeLite 从一组 C# 类 构建 .d.ts 接口文件。我 运行 遇到一个问题,其中某些 类 具有 DataMember 的属性,其中给定值与 属性 名称不同。在这种情况下,我希望 TypeLite 使用 DataMember 属性而不是 属性 名称 - 不幸的是,我在文档中找不到任何地方说这是可能的。
有什么想法吗?
code 仅检查内置 [TsProperty]
属性以重命名属性:
var attribute = memberInfo.GetCustomAttribute<TsPropertyAttribute>(false);
if (attribute != null) {
if (!string.IsNullOrEmpty(attribute.Name)) {
this.Name = attribute.Name;
}
this.IsOptional = attribute.IsOptional;
}
您可以简单地对其进行修补,使其也包括 [DataMember]
属性:
var dataMemberAttribute = memberInfo.GetCustomAttribute<System.Runtime.Serialization.DataMemberAttribute>(false);
if (dataMemberAttribute!= null) {
if (!string.IsNullOrEmpty(dataMemberAttribute.Name)) {
this.Name = dataMemberAttribute.Name;
}
this.IsOptional = !dataMemberAttribute.IsRequired;
}
也许您可以提交包含该修复程序的拉取请求。确保添加测试并考虑将两个属性应用于 属性.
的情况为了保持一致性,您还必须打补丁以支持 [DataContract]
属性才能重命名 类.