XML Serialization and MS / Mono Portability
I am trying to serialize classes using MS Runtime and Mono Runtime. When using MS runtime everything goes well, but with Mono I am giving me some exception and program run.
The following exception is thrown:
- An error has occurred reflecting the type: System.TypeInitializationException (class)
- An error has occurred reflecting type: System.InvalidOperationException (class)
- An error occured reflecting field: System.ArgumentOutOfRangeException <0 (array of classes)
The binary was compiled using MS SDK, but I don't think this is the problem.
What's going on? .NET shouln't be portable? How do I resolve these exceptions?
I have Mono for Windows installed, Monodevelop installed (just for the sake of it) and am having the same problems. Here is the class that is causing the problem:
[XmlInclude(typeof(XmlServiceVersion))]
[XmlInclude(typeof(XmlUserLogin))]
[XmlInclude(typeof(XmlNetService))]
[XmlInclude(typeof(XmlUserList))]
[XmlInclude(typeof(XmlGroupList))]
public class XmlMessage
{
...
[XmlAttribute]
public XmlMessageCode Code = XmlMessageCode.Invalid;
[XmlAttribute]
public XmlMessageError Error = XmlMessageError.None;
public Object Contents = null;
private static XmlSerializer mXmlSerialize = new XmlSerializer(typeof(XmlMessage));
}
Exception Text:
Unhandled Exception: System.TypeInitializationException: An exception was thrown by the type initializer for iGecko.XmlClientConfiguration ---> System.TypeInitializationException: An exception was thrown by the type initializer for iGecko.Net.XmlMessageClient ---> System.InvalidOperationException: There was an error reflecting type 'iGecko.Net.XmlMessage'. ---> System.InvalidOperationException: There was an error reflecting type 'iGecko.Net.XmlProcessList'. ---> System.InvalidOperationException: There was an error reflecting field 'Items'. ---> System.ArgumentOutOfRangeException: Cannot be negative.
Parameter name: length
I forgot to mention that this happens during the contructor, which is supposed to serialize the class above.
Update
Sorry for the "grandiose" editing, but I'm starting to understand the error. The XmlMessage contains a public Object field that can be assigned an XmlProcessList class that derives from the class described in this post ( XmlDictionary ) that defines the Items field.
a source to share
I had the same problem with nested generic types. The following class would throw the exact above exception in Mono while working in .NET:
[Serializable]
public class SerializableDictionary<TKey, TValue>
{
/// <summary>
/// List of serializable dictionary entries.
/// </summary>
[XmlElement("Entry")]
public List<KeyAndValue<TKey, TValue>> Entries { get; set; }
/// <summary>
/// Serializable key-value pair.
/// </summary>
[Serializable]
public class KeyAndValue
{
/// <summary>
/// Key that is mapped to a value.
/// </summary>
public TKey Key { get; set; }
/// <summary>
/// Value the key is mapped to.
/// </summary>
public TValue Value { get; set; }
}
}
whereas the following works:
[Serializable]
public class SerializableDictionary<TKey, TValue>
{
/// <summary>
/// List of serializable dictionary entries.
/// </summary>
[XmlElement("Entry")]
public List<KeyAndValue<TKey, TValue>> Entries { get; set; }
/// <summary>
/// Serializable key-value pair.
/// </summary>
[Serializable]
public class KeyAndValue<TKey, TValue>
{
/// <summary>
/// Key that is mapped to a value.
/// </summary>
public TKey Key { get; set; }
/// <summary>
/// Value the key is mapped to.
/// </summary>
public TValue Value { get; set; }
}
}
If the nested class uses its own type parameters fixed the problem for me in Mono, maybe this is for you too.
a source to share