简体   繁体   English

将接口类型对象保存到IsolatedStorageSettings中,进行序列化

[英]Save interface type object to IsolatedStorageSettings, serialization

I need to store list objects which implements IAnimal interface to Windows Phone 8 IsolatedStorageSettings.ApplicationSettings. 我需要存储实现IAnimal接口到Windows Phone 8 IsolatedStorageSettings.ApplicationSettings的列表对象。

My animal interface looks like this: 我的动物界面如下所示:

public interface IAnimal
{
    string Name { get; }
}

Then I have different animals which I want to store to IsolatedStorageSettings.ApplicationSettings. 然后,我有不同的动物要存储到IsolatedStorageSettings.ApplicationSettings中。

public class Cat : IAnimal
{
    string Name { get; set; }
}


public class Dog: IAnimal
{
    string Name { get; set; }
}

I have methods to get / set list of animals. 我有获取/设置动物清单的方法。

public IReadOnlyCollection<IAnimal> GetAnimals()
{
    return (List<IAnimal>)storage["animals"];
}

public void AddAnimal(IAnimal animal)
{
    List<IAnimal> animals = (List<IAnimal>)storage["animals"];
    animals.Insert(0, (IAnimal)animal);

    this.storage["animals"] = animals;
    this.storage.Save();
}

If I use these methods, I will get System.Runtime.Serialization.SerializationException, Element ' http://schemas.microsoft.com/2003/10/Serialization/Arrays:anyType ' contains data of the ' http://schemas.datacontract.org/2004/07/MyApp.Models:Cat ' data contract. 如果使用这些方法,则将获得System.Runtime.Serialization.SerializationException,元素“ http://schemas.microsoft.com/2003/10/Serialization/Arrays:anyType ”包含“ http:// schemas的数据” 。 datacontract.org/2004/07/MyApp.Models:Cat数据合同。 The deserializer has no knowledge of any type that maps to this contract. 解串器不知道任何映射到该合同的类型。 Add the type corresponding to 'Cat' to the list of known types - for example, by using the KnownTypeAttribute attribute or by adding it to the list of known types passed to DataContractSerializer. 将与“猫”相对应的类型添加到已知类型的列表中-例如,通过使用KnownTypeAttribute属性或将其添加到传递给DataContractSerializer的已知类型的列表中。

I have also tried to add KnownType attribute to Cat and Dog but with out success. 我也曾尝试将KnownType属性添加到Cat and Dog,但是没有成功。

Is this the proper way to store object to IsolatedStorageSettings, when I know only that the object implements certain interface? 当我仅知道对象实现某些接口时,这是将对象存储到IsolatedStorageSettings的正确方法吗?

I would suspect you are placing the KnownType attribute in the wrong place. 我怀疑您将KnownType属性放置在错误的位置。 When I have used it, I have always added it to a base class. 使用它后,我总是将其添加到基类中。

Example: 例:

public interface IAnimal
{
   string Name { get; }
}

  [KnownType(typeof(Cat))]
  [KnownType(typeof(Dog))]
public class Animal: IAnimal
{
  string Name { get; }
}

public class Cat : Animal
{
  string Name { get; set; }
}


public class Dog: Animal
{
  string Name { get; set; }
}

http://msdn.microsoft.com/en-us/library/ms751512(v=vs.110).aspx http://msdn.microsoft.com/zh-CN/library/ms751512(v=vs.110).aspx

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM