【发布时间】:2016-02-29 19:26:59
【问题描述】:
我正在编写一个简单的类来序列化我的IConfiguration 接口,该接口具有以下方法
IEnumerable<KeyValuePair<string, object>> GetAllProperties();
在我的课堂上我有一个方法
public void WriteConfiguration(IConfiguration data, Stream stream)
{
new Serializer().Serialize(new StreamWriter(stream), data.GetAllProperties());
}
但Serializer 不会向流中写入任何内容。我在某处读到 KeyValuePair 不可序列化,但现在不再是这种情况了(它在 .NET 2.0 中)
我首先尝试将IEnumerable 转换为List(使用.ToList()),但没有任何改变。然后我尝试创建一个类来代替KeyValuePair:
[Serializable]
private class Pair<TKey, TValue>
{
public Pair() { }
public Pair(TKey key, TValue value)
{
Key = key;
Value = value;
}
public TKey Key { get; set; }
public TValue Value { get; set; }
}
但还是不行。
【问题讨论】:
-
似乎对我有用...我怀疑这可能是因为您从未处理过 StreamWriter,因此它从未被刷新。
标签: c# serialization yamldotnet