I'm using a dictionary to hold some parameters and I've just found out that it's not possible to serialize anything that implements IDictionary (unable to serialize IDictionary).
As a workaround I'd like to convert may dictionary into a string for serialization and then convert back to a dictionary when required.
As I'm trying to improve my LINQ this seems like a good place to do it but I'm not sure how to start.
This is how I'd implement it without LINQ:
/// <summary>
/// Get / Set the extended properties of the FTPS processor
/// </summary>
/// <remarks>Can't serialize the Dictionary object so convert to a string (http://msdn.microsoft.com/en-us/library/ms950721.aspx)</remarks>
public Dictionary<string, string> FtpsExtendedProperties
{
get
{
Dictionary<string, string> dict = new Dictionary<string, string>();
// Get the Key value pairs from the string
string[] kvpArray = m_FtpsExtendedProperties.Split('|');
foreach (string kvp in kvpArray)
{
// Seperate the key and value to build the dictionary
string[] pair = kvp.Split(',');
dict.Add(pair[0], pair[1]);
}
return dict;
}
set
{
string newProperties = string.Empty;
// Iterate through the dictionary converting the value pairs into a string
foreach (KeyValuePair<string,string> kvp in value)
{
newProperties += string.Format("{0},{1}|", kvp.Key, kvp.Value);
}
// Remove the last pipe serperator
newProperties = newProperties.Substring(0, newProperties.Length - 1);
}
}
See Question&Answers more detail:
os 与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…