I'm trying to have classes serialized using MS runtime and Mono runtime. While using MS runtime everything goes fine, but using Mono I give me some exception and program startup.
The following exception are thrown:
- There was an error reflecting a type: System.TypeInitializationException (a class)
- There was an error reflecting a type: System.InvalidOperationException (a class)
- There was an error reflecting a field: System.ArgumentOutOfRangeException < 0 (an 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 to solve these exceptions?
I've installer MONO for Windows, installed Monodev开发者_如何学Pythonelop (just for the sake) and get the same problems. Here is a class which causes the problems:
[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));
}
The exception text is specifically:
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 happen during the contructor which should serialize the class above.
Update
Sorry for 'grand' editing, but I'm starting to understand the error. The XmlMessage contains an Object public field, which could be assigned an XmlProcessList class, which derives from the class described in this post (XmlDictionary), which defines the Items field.
I had the same problem with nested generic types. The following class would throw exactly the above exception in Mono while it's 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 one 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; }
}
}
Having the nested class use its own type parameters fixed the issue for me in Mono, maybe it does for you, as well.
Try running your application through MoMA (Mono migration analyser) - it should tell you what methods you are using that Mono does not support and any potential migration issues (i.e building paths without using Path.Combine
).
精彩评论