我有两个可序列化的类:
[Serializable]
public class Word
{
public List<string> similes;
}
[Serializable]
public class Lexicon
{
public List<Word> words;
}
这两个类分别存储为XML文件,但Lexicon的XML文件如下所示:
<Word>
<Similes>
<string>Hello</string>
<string>Hi</string>
</Similes>
</Word>
<Word>
<Similes>
<string>Goodbye</string>
<string>Bye</string>
</Similes>
</Word>
<Lexicon>
<Words>
<Word>
<Similes>
<string>Hello</string>
<string>Hi</string>
</Similes>
</Word>
<Word>
<Similes>
<string>Goodbye</string>
<string>Bye</string>
</Similes>
</Word>
</Words>
</Lexicon>
这只是两个字!您可以看到,随着更多单词的添加,这将很快失控,另外,“Hello,Hi”单词在反序列化时,是与反序列化词典中存储的单词不同的对象 - 当它们应该引用相同的对象时。
所以基本上有没有办法让序列化的Lexicon文件引用通过序列化Word实例而不是复制xml而生成的文件?
答案 0 :(得分:2)
您可以为可序列化Word
添加wordID,并在Lexicon类中引用此ID
[Serializable]
public class Word
{
public string WordID;
public List<string> similes;
}
你的词类......
<Word>
<WordID>1</WordID>
<Similes>
<string>Hello</string>
<string>Hi</string>
</Similes>
</Word>
词汇课
public class Lexicon
{
public List<string> wordIDs;
}
Lexicon可能就像
<Lexicon>
<WordIDs>
<string>1</string>
<string>2</string>
</WordIDs>
</Lexicon>