可能重复:
如何在.Net(特别是C#)中深层复制对象?
请看下面的代码(摘自C#书):
public class MyClass
{
public int val;
}
public struct myStruct
{
public int val;
}
public class Program
{
private static void Main(string[] args)
{
MyClass objectA = new MyClass();
MyClass objectB = objectA;
objectA.val = 10;
objectB.val = 20;
myStruct structA = new myStruct();
myStruct structB = structA;
structA.val = 30;
structB.val = 40;
Console.WriteLine("objectA.val = {0}", objectA.val);
Console.WriteLine("objectB.val = {0}", objectB.val);
Console.WriteLine("structA.val = {0}", structA.val);
Console.WriteLine("structB.val = {0}", structB.val);
Console.ReadKey();
}
}
Run Code Online (Sandbox Code Playgroud)
我知道它会产生下面的输出
objectA.val …Run Code Online (Sandbox Code Playgroud) 我需要在我的一个没有超类的对象中实现深度克隆.
处理CloneNotSupportedException超类抛出的检查的最佳方法是什么(是Object)?
一位同事建议我按以下方式处理:
@Override
public MyObject clone()
{
MyObject foo;
try
{
foo = (MyObject) super.clone();
}
catch (CloneNotSupportedException e)
{
throw new Error();
}
// Deep clone member fields here
return foo;
}
Run Code Online (Sandbox Code Playgroud)
这对我来说似乎是一个很好的解决方案,但我想把它扔到StackOverflow社区,看看是否还有其他任何我可以包含的见解.谢谢!
我想知道在C#中进行浅层复制的最快方法是什么?我只知道有两种方法可以做浅拷贝:
我发现(2)比(1)快.我想知道是否还有另一种方法可以进行浅层复制?
我正在尝试将一个项目从 gitlab 克隆到我的本地机器。我已被授予开发人员权限,并使用命令 'git clone
我收到的错误消息:
remote: The project you were looking for could not be found.
fatal: repository 'https://gitlab.com/KZA_Connected/skilltree.git/' not found
Run Code Online (Sandbox Code Playgroud)
任何帮助将不胜感激。
我一直在为项目创建对象,有些实例我必须为这些对象创建一个深层副本我已经想到了使用C#的内置函数,即MemberwiseClone().困扰我的问题是每当我创建一个新类时,我都必须编写一个类似下面代码的函数来进行浅拷贝.有人请帮助我改进这部分并给我一个更好的浅拷贝比第二行代码.谢谢 :)
SHALLOW COPY:
public static RoomType CreateTwin(RoomType roomType)
{
return (roomType.MemberwiseClone() as RoomType);
}
Run Code Online (Sandbox Code Playgroud)
深度复制:
public static T CreateDeepClone<T>(T source)
{
if (!typeof(T).IsSerializable)
{
throw new ArgumentException("The type must be serializable.", "source");
}
if (Object.ReferenceEquals(source, null))
{
return default(T);
}
IFormatter formatter = new BinaryFormatter();
Stream stream = new MemoryStream();
using (stream)
{
formatter.Serialize(stream, source);
stream.Seek(0, SeekOrigin.Begin);
return (T)formatter.Deserialize(stream);
}
}
Run Code Online (Sandbox Code Playgroud) 在过去,我需要克隆对象,只是发现它们没有实现Clone()方法,迫使我手动执行(创建一个新实例并将所有属性从原始实例复制到新实例)
为什么克隆不像复制分配对象的内存块那么容易,因此Clone在object类中有方法,.NET中的所有类都继承它?
鉴于以下课程:
class A
{
public List<B> ListB;
// etc...
}
Run Code Online (Sandbox Code Playgroud)
哪个B是可以继承/包含其他类的另一个类.
鉴于这种情况:
A 是一个大类,包含许多引用类型B,[Serializable]因为我无法访问源代码B以下执行深度复制的方法不起作用:
ICloneable或MemberwiseClone作为类A包含许多引用类型A,因为类很大并且不断被添加到,并且包含B无法深度复制的类(如)B,没有可用的源代码)[Serializable]我怎样才能深造课A?
我有下一种情况:
Connection manager应该每次都有一个对象ConnectionServer和新对象的DataBean
So,我已经创建了这些bean并配置了它的spring xml.
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:context="http://www.springframework.org/schema/context"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd">
<bean id="dataBena" class="com.test.DataBean" scope="prototype"/>
<bean id="servCon" class="com.test.ServerCon"/>
<!--<bean id="test" class="com.test.Test"/>-->
<context:component-scan base-package="com.test"/>
</beans>
Run Code Online (Sandbox Code Playgroud)
并添加范围prototype为DataBean
在此之后,我创建了名为Test的简单util/component类
@Component
public class Test {
@Autowired
private DataBean bean;
@Autowired
private ServerCon server;
public DataBean getBean() {
return bean.clone();
}
public ServerCon getServer() {
return server;
}
}
Run Code Online (Sandbox Code Playgroud)
但是,每次调用getBean()方法我都会克隆这个bean,这对我来说是个问题.我可以在没有克隆方法的情况下从spring配置中做到吗?谢谢.
我有一个带有一些子模块的项目.其中许多是从GitHub fork中克隆到的,我为自定义mod添加了一个分支.典型的设置如下:
在本地文件夹中:MyProject1/Frameworks/SomeAmazingRepo /
$ git branch -vva
*my-fork 123456 [my-fork/my-fork] Latest commit msg from fork
master abcdef [origin/master] Latest commit msg from original repo
remotes/my-fork/my-fork 123456 [my-fork/my-fork] Latest commit msg from fork
remotes/my-fork/master abcdef [origin/master] Latest commit msg from original repo
remotes/origin/HEAD -> origin/master
remotes/origin/master abcdef [origin/master] Latest commit msg from original repo
$ git remote -v
my-fork git@github.com:MyUser/SomeAmazingRepo.git (fetch)
my-fork git@github.com:MyUser/SomeAmazingRepo.git (push)
origin git://github.com/OriginalOwner/SomeAmazingRepo.git (fetch)
origin git://github.com/OriginalOwner/SomeAmazingRepo.git (push)
Run Code Online (Sandbox Code Playgroud)
我git clone --recursive我的项目开始一个新的分拆计划,当它开始递归,它吐出了一个错误,声称它找不到这些回购存储的提交.检查后,似乎没有添加遥控器,并且分支机构在主机中留下(空)...
在本地文件夹中:MyProject2/Frameworks/SomeAmazingRepo /
$ git branch …Run Code Online (Sandbox Code Playgroud) 编辑2:下面是一个基于DuffyMo响应的代码片段,演示了如何使用System.arraycopy克服多维数组克隆的局限性.
import java.util.Arrays;
public class Randar {
public static int[][] arrayMaster = {{6,1}, {10,1}, {1,1}};
private static int[][] arrayChanges = new int[arrayMaster.length][2];
public Randar () {
}
public static void main(String[] args) {
arrayChanges[0][0] = 0;
resetArrays(arrayChanges, arrayMaster);
arrayChanges[0][0] = 0;
System.out.format("arrayMaster: %s, arrayChanges: %s", Arrays.deepToString(arrayMaster), Arrays.deepToString(arrayChanges));
}
public static void resetArrays(int[][] arrayChanges, int[][] arrayMaster) {
for (int a=0; a< arrayMaster.length; a++) {
System.arraycopy(arrayMaster[a], 0, arrayChanges[a], 0, arrayMaster[a].length);
}
// arrayChanges = arrayMaster.clone(); will NOT work as expected
}
} …Run Code Online (Sandbox Code Playgroud)