XML 序列化和 MS/Mono 可移植性

发布于 2024-09-02 06:06:42 字数 1966 浏览 4 评论 0原文

我正在尝试使用 MS 运行时和 Mono 运行时序列化类。使用 MS 运行时一切正常,但使用 Mono 时出现一些异常和程序启动。

抛出以下异常:

  • 存在反映类型的错误:System.TypeInitializationException(类)
  • 存在反映类型的错误:System.InvalidOperationException(类)
  • 存在反映字段的错误:System.ArgumentOutOfRangeException < 0(类数组)

二进制文件是使用 MS SDK 编译的,但我不认为这是问题所在。

这是怎么回事? .NET 不应该是可移植的吗?如何解决这些异常呢?


我已经安装了 Windows 版 MONO,安装了 Monodevelop(只是为了方便)并遇到了同样的问题。这是导致问题的类:

[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));

}

异常文本具体如下:

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

我忘记提及这种情况发生在应该序列化上面的类的构造函数期间。


更新

抱歉进行了“大”编辑,但我开始理解这个错误。 XmlMessage 包含一个 Object 公共字段,可以为该字段分配一个 XmlProcessList 类,该类派生自本文中描述的类 (XmlDictionary),它定义 Items 字段。

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 Monodevelop (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.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

二智少女猫性小仙女 2024-09-09 06:06:42

我对嵌套泛型类型也有同样的问题。当它在 .NET 中工作时,下面的类将在 Mono 中抛出上述异常:

[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; }
    }
}

而下面的类则有效:

[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; }
    }
}

让嵌套类使用它自己的类型参数在 Mono 中为我解决了这个问题,也许它也为你解决了这个问题。

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.

街道布景 2024-09-09 06:06:42

尝试通过 MoMA (Mono 迁移分析器)运行您的应用程序 - 它应该告诉您您使用的是什么方法使用 Mono 不支持任何潜在的迁移问题(即在不使用 Path.Combine 的情况下构建路径)。

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).

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文