它可以序列化(通过插入自定义__type属性)
public IAsset Content;
但不是
public List<IAsset> Contents;
– 列表在序列化数据中显示为空.有没有办法做到这一点 – 序列化接口类型列表?
该应用程序是大而老的,它使用的对象的形状可能不会被允许改变.
谢谢
你可能不需要做太多:)
The JsON and JsV Text serializers Now support serializing and
deserializing DTOs with Interface / Abstract or object types. Amongst
other things,this allows you to have an IInterface property which
when serialized will include its concrete type information in a __type
property fIEld (similar to other JsON serializers) which when
serialized populates an instance of that concrete type (provIDed it
exists).[…]
Note: This feature is automatically added to all
Abstract/Interface/Object types,i.e. you don’t need to include any
[KNownType] attributes to take advantage of it.
不多:
public interface IAsset{ string Bling { get; set; }}public class AAsset : IAsset{ public string Bling { get; set; } public overrIDe string ToString() { return "A" + Bling; }}public class BAsset : IAsset{ public string Bling { get; set; } public overrIDe string ToString() { return "B" + Bling; }}public class AssetBag{ [JsonProperty(TypenameHandling = TypenameHandling.None)] public List<IAsset> Assets { get; set; } }class Program{ static voID Main(string[] args) { try { var bag = new AssetBag { Assets = new List<IAsset> {new AAsset {Bling = "Oho"},new BAsset() {Bling = "Aha"}} }; string Json = JsonConvert.SerializeObject(bag,new JsonSerializerSettings() { TypenameHandling = TypenameHandling.auto }); var anotherBag = JsonConvert.DeserializeObject<AssetBag>(Json,new JsonSerializerSettings() { TypenameHandling = TypenameHandling.auto });@H_404_2@ 总结
以上是内存溢出为你收集整理的c# – 使用ServiceStack.Text序列化接口类型列表全部内容,希望文章能够帮你解决c# – 使用ServiceStack.Text序列化接口类型列表所遇到的程序开发问题。
如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)