你如何使数组持久化? [英] How do you make an array persistent?

查看:70
本文介绍了你如何使数组持久化?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个数组,我希望保留为只读。数组

保存对由new实例化的变量myObjects的引用

一直在变化。


变量是一部分我使用ICloneable的类,即

" Clone();" (这个

特定类的深度和/或浅拷贝的工作原理相同)。


使用ICloneable,我能够成功复制

变量如下:

myObjectsArray [j] =(MyObjects)X.Clone();其中X是foreach

(myObjectsArray中的myObject X)


一切正常,除了一件事:我只能保留

a使用IClone一次复制数组。在那之后,阵列是没有好处的。
。但实际上我很高兴即使发生了这种情况,因为我的目的是使用myObjects回滚我的程序状态只需

一次。所以该计划有效。但是我想知道将来的参考

如何使myObjectsArray持久化。


我是否只是让它'只读'?但这似乎只适用于整数,

双打等等?


我是否使用memento的模板(在很多书中找到)设计

pattern" (用于回滚)?对于这个简单的问题,它看起来很复杂,有点矫枉过正。


我有可能搞砸了我如何通过''只读

数组,myObjectsArray []。


这是我如何使用它(也许我不由自主地改变它?)

public void Rollback(myObjectsArray [] OASnapshot)

{


if(myObjectsList.Count!= 0){myObjectsList.Clear(); } //

清单


myObjectsList = new

List< myObjects>(OASnapshot.Length); /


myObjectsList.AddRange(OASnapshot); // **

}


// **这可以用
$ b中的''old''值重新填充List $ b OASnapshot,这正是我想要的。但是,我第二次调用这个方法回滚,稍后当对象改变了
值时,OASnapshot似乎没有任何''旧的''信息

的对象,正如我想的那样,但事实上,OASnapshot似乎与myObjectsList中的信息完全相同。不知何故

OASnapshot没有保留旧值(第二次)。

我尝试使用''ref''来表示OASnapshot但同样的事情。我觉得

发生的事情是因为垃圾收集,不知怎的OASnapshot是

不持久。


无论如何,这是不是和任何人一起打铃?


RL

I have an array that I wish to preserve as "read only". The array
holds references to variables myObjects instantiated by new that
change all the time.

The variables are part of a class that I used ICloneable on, namely
"Clone();" (deep and/or shallow copies worked the same for this
particular class).

Using ICloneable, I am able to successfully make a copy of the
variables like so:

myObjectsArray[j] = (MyObjects)X.Clone(); where X is foreach
(myObject X in myObjectsArray)

Everything is working fine except for one thing: I can only preserve
a copy of the array using IClone one time. After that, the array is
no good. But actually I''m quite happy even that this occurs, since my
purpose was to rollback the state of my program using myObjects just
once. So the program works. But I want to know for future reference
how to make the myObjectsArray persistent.

Do I make it ''readonly''? But this only seems to work for ints,
doubles, etc?

Do I use the template (found in a lot of books) of "memento design
pattern" (used in rollbacks)? It looks complicated, a bit of overkill
for this simple problem.

There is a chance that I''m messing up how I pass the ''read only"
array, myObjectsArray[].

Here is how I use it (perhaps I change it involuntarily?)
public void Rollback (myObjectsArray[] OASnapshot)
{

if (myObjectsList.Count != 0) { myObjectsList.Clear(); } //
clear list

myObjectsList = new
List<myObjects>(OASnapshot.Length); /

myObjectsList.AddRange(OASnapshot); //**
}

//** this works to repopulate the List with the ''old'' values in
OASnapshot, which is exactly what I want. However, the second time I
call this method "Rollback", later on when the objects have changed
values, OASnapshot does not seem to have any of the ''old'' information
of the objects, as I want, but in fact, OASnapshot seems to have
exactly the same information found in myObjectsList. Somehow
OASnapshot is not holding the old values (the second time around).
I tried using ''ref'' for OASnapshot but same thing. I think what is
happening is that because of garbage collection, somehow OASnapshot is
not persistent.

Anyway, does this ring a bell with anybody?

RL

推荐答案

好吧,我说的太快了。


原来我毕竟没有真正的解决方案(我假设在

a快速检查之后也是如此,但现在还为时过早了。)

尽管我尽力调整程序,但使用Clone和Clone。而不是

复制数组的所有地方,使用''new''用于数组并执行数组

元素元素克隆从一个数组复制到另一个数组(甚至三个

阵列连续,A-> B-> C,徒劳地试图远离

可怕的原始参考),使用深层复制进行克隆

函数,并使我认为可以继承的每一个类

ICloneable,我不能让数组在它之前被多次读取

改变状态。也就是说,撤消只工作一次。


我认为解决方案就像Marc Gravell所说的那样,这涉及

如我所示设置纪念品设计模式在OP中。


另一种解决方案,实际上并不是解决方案,是将要重建的原始数据类型分解为原始数据类型(int ,浮动,

等),保存这些数据,并使用这些数据重建状态,

使用每个交互类中的函数和程序

指令。但这种解决方案是一项艰苦的工作,就像设置纪念品设计模式一样难以实现。事实上,如果你不得不采用如此激烈的解决方案,那么它几乎会失败撤消的目的

(因为撤消之后你必须重建现在状态

)。


我认为这是C#的缺陷。你应该可以更容易地做到这一点

- 应该有一个库函数,它将保存在

不可变形式中,所有对可变对象的引用都允许你

按原样重建可变对象。当然memento

设计模式就是这样一个解决方案,但我所说的是这个

memento设计模式应该作为语言功能构建到C#中。

我故意听说C#设计师,不像C ++那样,没有
为参考对象引入''const''(常量)关键字/

变量,但这种刻意的设计选择是IMO的缺陷。一个

''const''关键字对解决这个问题大有帮助,

虽然我确定一些大师会指出IL引擎在工作

背景会被''const''参考混淆,因为东西

在幕后发生变化,没有什么是不变的,除了

堆栈上的未装箱的原始数据类型(int,double,bool)。

RL

Well, I spoke too soon.

Turns out I didn''t really have a solution after all (I assume so after
a quick check but it was premature).

Despite my best efforts tweaking the program, using "Clone" instead of
copy everywhere for arrays, using ''new'' for arrays and doing a array
element by element clone copy from one array to another (even three
arrays in a row, A->B->C, in a vain attempt to get away from the
dreaded original reference), using Deep Copying for the Clone
function, and making every class I could think of as inheriting
ICloneable, I cannot get the array to be read more than once before it
changes state. That is, undo works just once.

I think the solution is like Marc Gravell said, and that involves
setting up a memento design pattern as I indicated in the OP.

Another solution, which is not really a solution, is to break down the
state you want to reconstruct into primitive data types (int, float,
etc), save this data, and ''reconstruct the state'' using this data,
using functions in each of the interacting classes and as the program
dictates. But that ''solution'' is hard work, just as hard or harder
than setting up a memento design pattern. In fact it nearly defeats
the purpose of ''undo'' if you have to resort to such a drastic solution
(because after the undo you have to reconstruct the present state
too).

I consider this a defect of C#. You should be able to do this more
easily--there should be a library function that will preserve, in
immutable form, all references to mutable objects allowing you to
reconstruct the mutable objects as they were. Of course the memento
design pattern is such a solution, but what I''m saying is that this
memento design pattern should be built into C# as a language feature.
I heard the C# designers deliberately, unlike in C++, did not
introduce the ''const'' (constant) keyword for reference objects/
variables, but this deliberate design choice is a defect IMO. A
''const'' keyword would go a long way towards solving this problem,
though I''m sure some guru will point out that the IL engine working in
the background will be confused by a ''const'' reference, since stuff
changes behind the scenes and nothing is constant save perhaps the
unboxed primitive data types (int, double, bool) on the stack.

RL


8月9,5:26 * am,raylopez99< raylope ... @ yahoo.comwrote:
On Aug 9, 5:26*am, raylopez99 <raylope...@yahoo.comwrote:

好​​吧,我说得太早。

事实证明我毕竟没有真正的解决方案(我假设在

a之后快速检查但是它还为时过早)。
Well, I spoke too soon.

Turns out I didn''t really have a solution after all (I assume so after
a quick check but it was premature).



我发现了一个''解决方案''并且它相当不优雅 - 你只需要继续制作你想要的状态的多个副本,然后将它推入一个堆栈。

然后在需要时从堆栈中弹出它o撤消。


这与我在第二篇文章中所说的基本相同,但是你没有使用数组b $ b或列表,因为

Predicate而导致问题。 (这是一个复杂的课程,你需要设置

谓词/动作< T因为默认显然不是

工作 - 太麻烦了,只是使用堆栈)


尽管如此,将内置于C#语言中的内容用于读取而非依赖于不优雅的程序员解决方案,这将是一件好事。 。


RL

I found a ''solution'' and it is rather inelegant--you just keep making
multiple copies of the state you want, then Push it into a Stack.
Then you Pop it from the stack when you want to "undo".

It''s basically the same as what I said in my second post, but you
don''t use arrays or lists, which were causing problems because of the
"Predicate" (this was a complicated class, and you needed to set up
the Predicate/Action <Tbecause the default apparently was not
working--too much bother, just use Stack)

Still, it would be nice to have something ''built into'' the C# language
for readonly instead of depending on a inelegant programmers solution.

RL





Ray,

序列化并将其写入磁盘。


Cor


" raylopez99" < ra ******** @ yahoo.comschreef在bericht

新闻:1e ********************** ************ @ m3g2000h sc.googlegroups.com ...


Ray,

Serialize it and write it to disk.

Cor

"raylopez99" <ra********@yahoo.comschreef in bericht
news:1e**********************************@m3g2000h sc.googlegroups.com...

>我有一个数组,我希望保留为只读。数组

保存对由new实例化的变量myObjects的引用

一直在变化。


变量是一部分我使用ICloneable的类,即

" Clone();" (这个

特定类的深度和/或浅拷贝的工作原理相同)。


使用ICloneable,我能够成功复制

变量如下:

myObjectsArray [j] =(MyObjects)X.Clone();其中X是foreach

(myObjectsArray中的myObject X)


一切正常,除了一件事:我只能保留

a使用IClone一次复制数组。在那之后,阵列是没有好处的。
。但实际上我很高兴即使发生了这种情况,因为我的目的是使用myObjects回滚我的程序状态只需

一次。所以该计划有效。但是我想知道将来的参考

如何使myObjectsArray持久化。


我是否只是让它'只读'?但这似乎只适用于整数,

双打等等?


我是否使用memento的模板(在很多书中找到)设计

pattern" (用于回滚)?对于这个简单的问题,它看起来很复杂,有点矫枉过正。


我有可能搞砸了我如何通过''只读

数组,myObjectsArray []。


这是我如何使用它(也许我不由自主地改变它?)


public void Rollback(myObjectsArray [] OASnapshot)

{


if(myObjectsList.Count!= 0){myObjectsList.Clear() ; } //

清单


myObjectsList = new

List< myObjects>(OASnapshot.Length); /


myObjectsList.AddRange(OASnapshot); // **

}


// **这可以用
$ b中的''old''值重新填充List $ b OASnapshot,这正是我想要的。但是,我第二次调用这个方法回滚,稍后当对象改变了
值时,OASnapshot似乎没有任何''旧的''信息

的对象,正如我想的那样,但事实上,OASnapshot似乎与myObjectsList中的信息完全相同。不知何故

OASnapshot没有保留旧值(第二次)。


我尝试使用''ref''作为OASnapshot但同样的事情。我觉得

发生的事情是因为垃圾收集,不知怎的OASnapshot是

不持久。


无论如何,这是不是和任何人敲响钟声?


RL
>I have an array that I wish to preserve as "read only". The array
holds references to variables myObjects instantiated by new that
change all the time.

The variables are part of a class that I used ICloneable on, namely
"Clone();" (deep and/or shallow copies worked the same for this
particular class).

Using ICloneable, I am able to successfully make a copy of the
variables like so:

myObjectsArray[j] = (MyObjects)X.Clone(); where X is foreach
(myObject X in myObjectsArray)

Everything is working fine except for one thing: I can only preserve
a copy of the array using IClone one time. After that, the array is
no good. But actually I''m quite happy even that this occurs, since my
purpose was to rollback the state of my program using myObjects just
once. So the program works. But I want to know for future reference
how to make the myObjectsArray persistent.

Do I make it ''readonly''? But this only seems to work for ints,
doubles, etc?

Do I use the template (found in a lot of books) of "memento design
pattern" (used in rollbacks)? It looks complicated, a bit of overkill
for this simple problem.

There is a chance that I''m messing up how I pass the ''read only"
array, myObjectsArray[].

Here is how I use it (perhaps I change it involuntarily?)
public void Rollback (myObjectsArray[] OASnapshot)
{

if (myObjectsList.Count != 0) { myObjectsList.Clear(); } //
clear list

myObjectsList = new
List<myObjects>(OASnapshot.Length); /

myObjectsList.AddRange(OASnapshot); //**
}

//** this works to repopulate the List with the ''old'' values in
OASnapshot, which is exactly what I want. However, the second time I
call this method "Rollback", later on when the objects have changed
values, OASnapshot does not seem to have any of the ''old'' information
of the objects, as I want, but in fact, OASnapshot seems to have
exactly the same information found in myObjectsList. Somehow
OASnapshot is not holding the old values (the second time around).
I tried using ''ref'' for OASnapshot but same thing. I think what is
happening is that because of garbage collection, somehow OASnapshot is
not persistent.

Anyway, does this ring a bell with anybody?

RL


这篇关于你如何使数组持久化?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆