如何重命名子类中继承的 API 成员?
How to rename an inherited API member in a subclass?
假设我有:
public class Parent{
[ApiMember(Name = "parentItem")]
public string Item {get; set;}
}
和
public class Child : Parent {
[ApiMember(Name = "childItem")]
public new string Item {get; set;}
}
既然父class中的'Item'属性应该隐藏,为什么用{"childItem": "something"}
returnsCould not find property childItem on RequestObject
发出请求?也就是说,在子 class 中重命名继承的 API members/properties 的最佳方法是什么(或者有没有办法)?
尝试在父级中使 属性 虚拟,然后在子级 class 中简单地覆盖它(没有 new
关键字):
public class Parent{
[ApiMember(Name = "parentItem")]
public virtual string Item {get; set;}
}
public class Child : Parent {
[ApiMember(Name = "childItem")]
public override string Item {get; set;}
}
[DataContract]
和 [DataMember]
属性会影响序列化,而 [Api*]
属性仅用于记录和添加有关您的 API 的额外元数据,但它不会影响序列化行为。
因此您应该将 DTO 更改为:
[DataContract]
public class Parent
{
[DataMember(Name = "parentItem")]
public virtual string Item { get; set; }
}
[DataContract]
public class Child : Parent
{
[DataMember(Name = "childItem")]
public override string Item { get; set; }
}
在大多数 ServiceStack 的序列化程序中序列化时将在何处使用它们,例如:
var json1 = new Parent { Item = "parent" }.ToJson();
json1.Print();
var json2 = new Child { Item = "child" }.ToJson();
json2.Print();
输出:
{"parentItem":"parent"}
{"childItem":"child"}
假设我有:
public class Parent{
[ApiMember(Name = "parentItem")]
public string Item {get; set;}
}
和
public class Child : Parent {
[ApiMember(Name = "childItem")]
public new string Item {get; set;}
}
既然父class中的'Item'属性应该隐藏,为什么用{"childItem": "something"}
returnsCould not find property childItem on RequestObject
发出请求?也就是说,在子 class 中重命名继承的 API members/properties 的最佳方法是什么(或者有没有办法)?
尝试在父级中使 属性 虚拟,然后在子级 class 中简单地覆盖它(没有 new
关键字):
public class Parent{
[ApiMember(Name = "parentItem")]
public virtual string Item {get; set;}
}
public class Child : Parent {
[ApiMember(Name = "childItem")]
public override string Item {get; set;}
}
[DataContract]
和 [DataMember]
属性会影响序列化,而 [Api*]
属性仅用于记录和添加有关您的 API 的额外元数据,但它不会影响序列化行为。
因此您应该将 DTO 更改为:
[DataContract]
public class Parent
{
[DataMember(Name = "parentItem")]
public virtual string Item { get; set; }
}
[DataContract]
public class Child : Parent
{
[DataMember(Name = "childItem")]
public override string Item { get; set; }
}
在大多数 ServiceStack 的序列化程序中序列化时将在何处使用它们,例如:
var json1 = new Parent { Item = "parent" }.ToJson();
json1.Print();
var json2 = new Child { Item = "child" }.ToJson();
json2.Print();
输出:
{"parentItem":"parent"}
{"childItem":"child"}