问题标签 [icloneable]

For questions regarding programming in ECMAScript (JavaScript/JS) and its various dialects/implementations (excluding ActionScript). Note JavaScript is NOT the same as Java! Please include all relevant tags on your question; e.g., [node.js], [jquery], [json], [reactjs], [angular], [ember.js], [vue.js], [typescript], [svelte], etc.

0 投票
1 回答
454 浏览

c# - C# 类、子类、IClonable、IDisposable

我正在使用 C# 进行一些练习,但我不了解大局。如果这些应该在 Java 中实现,我不会有问题。但是我是 C# 的新手,所以我把事情搞混了,我不知道结构应该是什么样子。我读了一些解释,例如:http: //www.codeproject.com/Articles/15360/Implementing-IDisposable-and-the-Dispose-Pattern-Phttp://msdn.microsoft.com/en-us/library/ fs2xkftw.aspx

我有第一堂课,我制作 LinkedList,它的类型是节点,所以: LinkedList linkedlist = new LinkedList 比我在 puti 中通过一些方法在链表中创建一些节点,这里还有方法 print,它从链表中打印元素。

我的“对象”类是 Node,其中有构造函数、属性以及 getter 和 setter。我这里也有virtula方法打印,我不知道我应该在这个方法中放什么,因为这个节点类只是抽象类,在这里我必须实现 IDisposable 方法:

在这里我不明白为什么要使用 IDispose?在我看到的所有示例中,它已被用于访问某些文件和处理图片。在我看到的一些例子中:

我不知道我是否必须把它放在一些新的类中,我应该把它放在同一个类中,甚至不实现它。

然后我创建了使用超类节点的子类 NodeLong 和 Nodestring,我放置了一些新的属性以及 setter 和 getter。我也使用了打印方法。我也不懂泛型。

线性集合类:

我的 Nodeelemts 课程:

如果我制作 NodeLang:IClonable 比我不能制作 NodeLang:Node。

我的指令说我必须在两个子类中使用 IClonable 方法,我不知道我应该如何使用它?在类 Node 中也使用 IDisposable,如何?我希望我能理解吗?

所以最后的问题是它应该如何看起来像我的类、子类、IDisposable 和 ICloneable?

0 投票
4 回答
10289 浏览

c# - 将 .NET 库移植到 PCL 时如何解决缺少 ICloneable 接口的问题?

我正在将现有的 .NET 类库移植到可移植类库。.NET 库广泛使用ICloneable接口,该接口不包含在可移植子集中。

通常,我在 .NET 类库中会遇到这样的类定义:

我该怎么做才能成功将此代码移植到可移植类库?我是否必须重写Clone方法调用,或者是否有不那么侵入性的解决方法?

我不能简单地删除泛型类型约束where T : ICloneable,因为那样该Bar方法将无法编译。

我可以编写一个替换接口来使用,而不是ICloneable在移植的代码中:

只要我只Foo<T>用实现的类进行实例化,这将起作用IPCLCloneable,但它不适用于例如来自实现的核心库的类型ICloneable,例如Array

(为了完整起见,应该指出,该ICloneable接口没有在可移植子集核心库中显式实现,因为它不存在,但该object Clone()方法确实存在于可移植子集Array类中,因此对于数组实现,例如int[].)

我还有什么其他选择?

0 投票
3 回答
1255 浏览

c# - 为什么 String.Clone() 返回原始字符串而不是它的副本?

令人惊讶的是,String.Clone()它不会像那样返回字符串的副本String.Copy()。相反,它返回'this'原始字符串。

我想了解 .Net Framework 团队为何选择这种方式。

根据MSDN

ICloneable 接口 [...] 要求您的 Clone 方法实现返回当前对象实例的副本。

String.Clone()显然不遵循此准则。

我知道字符串是不可变的,但如果不可变是这里的原因,它String.Copy()也会返回this,但它不会。

当然,这是一个相当理论的问题。

0 投票
7 回答
105532 浏览

c# - 实现 ICloneable 的正确方法

ICloneable在类层次结构中实现的正确方法是什么?假设我有一个抽象类DrawingObject。另一个抽象类RectangularObject继承自DrawingObject. 然后有多个具体类,如,Shape等,它们都继承自. 我想实现,然后将其沿层次结构向下传递,复制每个级别的可用属性并在下一个级别调用父级。TextCircleRectangularObjectICloneableDrawingObjectClone

然而问题是,由于前两个类是抽象的,我不能在Clone()方法中创建它们的对象。因此,我必须在每个具体类中复制属性复制过程。或者,还有更好的方法?

0 投票
2 回答
2712 浏览

c# - ICloneable Interface

This relates to the "Watson et al: Beginning Visual C# Chapter 10: exercise 4": Implement the ICloneable interface on the People class to provide deep copying capability

In the Person class we have:

To test it in Program.cs:

This works, and the original values of "Mick" are preserved. But the question is to the point of implementing ": ICloneable" on the Person and People class in the first place. The code works the same with or without it.

A related question is to the efficacy of what they call running a "recursive" implementation of ICloneable in their example

Have attempted miserably to get this recursion to work, but all we end up with is a StackOverflow. Short of using a global/static variable to quit the loop, does there exist an elegant way to implement this "recursively?"

0 投票
2 回答
192 浏览

c# - Resharper、ICloneable 且永不为空

Resharper 抱怨下面的代码,说最后的空检查是多余的,因为“表达式总是假的”:

它怎么知道它永远不能为空?

0 投票
1 回答
663 浏览

c# - Silverlight 中的对象深拷贝

我试图在silverligth 5中创建对象的副本,其中 IFormatters 和 IcCloanble 等接口不支持。*

我的对象是这样的:(请注意,这些对象是在反序列化 xml 时获得的):我尝试像这样复制:

当然它不起作用,然后我在谷歌上搜索到了这个代码:

但是silverligth5中的问题是:

Silverlight 5 中是否有任何替代方案。请详细说明。非常感谢。

0 投票
1 回答
1476 浏览

vb.net - 如何使派生类可克隆?

我正在尝试创建一个都实现的基类和派生类ICloneable。在我看来,基类Clone方法应该照顾所有基类属性和派生类Clone方法所有派生类属性并使用基类的Clone方法。结果是这样的:-

这导致Unable to cast object of type MyBaseClass to type MySubClass第 (1) 行出现运行时异常,我承认这并不让我感到惊讶。将该行替换为

产生了编译错误。在我看来,我需要某种将 a 转换为的方法MyBaseClassMySubClass以便使用基类的Clone. 一个不太吸引人的替代方案是将基类的成员复制到子类的Clone. 有谁知道在这里做什么?

我已经查看了这里的问题,但无法将其转换为 VB.Net。如果这里给出的解决方案是必要的,那么我将放弃这个想法。MustInherit如果有什么不同的话,我计划在时间充裕的情况下制作基类。

0 投票
1 回答
288 浏览

vb.net - VB.Net clone hierarchy where parent has reference to child and vice versa -> circular reference

This is the situation:

The actual object is more complex, having several levels. I'm not sure how to solve this because, at the moment, whenever you call Clone on the parent A class, you will end up with a circular reference.

How can I avoid this situation? Should I create my own Clone function and pass along a parameter?

0 投票
1 回答
299 浏览

wcf - 通过 WCF 传递继承的噩梦

我们在 WCF 服务中有一个类,如下所示

在添加服务引用的客户端,我在对象浏览器中打开引用,但它没有将 ICloneable 作为继承的接口传递,如“IExtensibleDataObject”

有谁知道我怎样才能通过它?