增加反序列化 xml 文件的类数组大小

发布于 2024-08-21 17:50:21 字数 365 浏览 6 评论 0原文

我有一个 XML 文件,我将其反序列化为类对象。该类包含许多其他类,其中一些是数组。

我想增加一些对象的数组大小。

我该怎么做?

在下面的示例中,MyData 对象的数组大小为 5,但 MyArrayClass 的数组大小仅为 1。

        for (int i = 0; i < 5; i++)
        {
            MyMainClass.MyAnotherClass.MyArrayClass[i] = MyData[i];
        }

因此,由于 MyArrayClass 的原始数组大小,此代码会失败。我如何增加它的大小?

谢谢。

I have an XML file that I deserialize into a class object. This class contains a number of other classes, some of which are arrays.

I would like to increase the array size of some of the objects.

How can I do this?

In the following example, the MyData object has an array size of 5 but the MyArrayClass say only has an array size of 1.

        for (int i = 0; i < 5; i++)
        {
            MyMainClass.MyAnotherClass.MyArrayClass[i] = MyData[i];
        }

This code thus fails because of the original array size of MyArrayClass. How do I increase it's size?

thank you.

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

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

发布评论

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

评论(3

又爬满兰若 2024-08-28 17:50:21

C# 有管理内存 - 只需将其删除为一个新的数组,其大小可以是你想要的。如果您不需要数据,垃圾收集器将为您清理旧的数据。如果这样做,则创建一个您想要的大小的新数据,并以您喜欢的方法复制所有旧数据,然后用新数据分配旧数据,然后让垃圾收集器清理旧数据一。

C# has manager memory - just delcare it a new array with size whatever you want. The garbage collector will clean up the old one for you if you don't need the data. If you do, then create a new one of the size you want, and copy all of the old data over in which ever method you prefer, then just assign over the old one with the new one and let the garbage collector clean up the old one.

在梵高的星空下 2024-08-28 17:50:21

我不太了解 C# 反序列化,但它必须是数组吗?您可以使用某种类型的列表来代替吗?这样,您就不必处理尺寸问题。

I'm not very up on my C# deserialization, but does it have to be an array? Could you use some type of list instead? That way, you don't have to deal with the sizing.

寄人书 2024-08-28 17:50:21

List 应该可以正常工作;在对象 API 上使用数组只会带来痛苦:

using System;
using System.Collections.Generic;
using System.IO;
using System.Xml.Serialization;
public class Foo
{
    private List<Bar> bars = new List<Bar>();
    public List<Bar> Bars { get { return bars; } }
}

public class Bar {
    public string Name { get; set; }
}

static class Program
{
    static void Main()
    {
        Foo orig = new Foo { Bars = { new Bar { Name = "abc"},
            new Bar { Name = "def"}}}, clone;
        using(MemoryStream ms = new MemoryStream()) {
            XmlSerializer ser = new XmlSerializer(orig.GetType());
            ser.Serialize(ms, orig);
            ms.Position = 0;
            clone = (Foo)ser.Deserialize(ms);
        }
        clone.Bars.Add(new Bar { Name = "ghi" });
        foreach (Bar bar in clone.Bars)
        {
            Console.WriteLine(bar.Name);
        }
    }
}

或者,您可以编写一个附加扩展方法,但这不是操作数据列表的有效方法:

using System;
public class Foo
{
    public Bar[] Bars { get; set; }
}

public class Bar {
    public string Name { get; set; }
}

static class Program
{
    static T[] Append<T>(this T[] arr, T value)
    {
        Array.Resize<T>(ref arr, arr == null ? 1 : (arr.Length + 1));
        arr[arr.Length - 1] = value;
        return arr;
    }
    static void Main()
    {
        Foo obj = new Foo { Bars = new[] { new Bar { Name = "abc" }, new Bar { Name = "def" } } };
        obj.Bars = obj.Bars.Append(new Bar { Name = "ghi" });
        foreach (Bar bar in obj.Bars)
        {
            Console.WriteLine(bar.Name);
        }
    }
}

A List<T> should work just fine; having arrays on the object API is just going to cause pain:

using System;
using System.Collections.Generic;
using System.IO;
using System.Xml.Serialization;
public class Foo
{
    private List<Bar> bars = new List<Bar>();
    public List<Bar> Bars { get { return bars; } }
}

public class Bar {
    public string Name { get; set; }
}

static class Program
{
    static void Main()
    {
        Foo orig = new Foo { Bars = { new Bar { Name = "abc"},
            new Bar { Name = "def"}}}, clone;
        using(MemoryStream ms = new MemoryStream()) {
            XmlSerializer ser = new XmlSerializer(orig.GetType());
            ser.Serialize(ms, orig);
            ms.Position = 0;
            clone = (Foo)ser.Deserialize(ms);
        }
        clone.Bars.Add(new Bar { Name = "ghi" });
        foreach (Bar bar in clone.Bars)
        {
            Console.WriteLine(bar.Name);
        }
    }
}

Alternatively, you could write an append extension method, but this is not an efficient way to manipulate lists of data:

using System;
public class Foo
{
    public Bar[] Bars { get; set; }
}

public class Bar {
    public string Name { get; set; }
}

static class Program
{
    static T[] Append<T>(this T[] arr, T value)
    {
        Array.Resize<T>(ref arr, arr == null ? 1 : (arr.Length + 1));
        arr[arr.Length - 1] = value;
        return arr;
    }
    static void Main()
    {
        Foo obj = new Foo { Bars = new[] { new Bar { Name = "abc" }, new Bar { Name = "def" } } };
        obj.Bars = obj.Bars.Append(new Bar { Name = "ghi" });
        foreach (Bar bar in obj.Bars)
        {
            Console.WriteLine(bar.Name);
        }
    }
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文