增加反序列化 xml 文件的类数组大小
我有一个 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
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.
我不太了解 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.
List
应该可以正常工作;在对象 API 上使用数组只会带来痛苦:或者,您可以编写一个附加扩展方法,但这不是操作数据列表的有效方法:
A
List<T>
should work just fine; having arrays on the object API is just going to cause pain:Alternatively, you could write an append extension method, but this is not an efficient way to manipulate lists of data: