用子类复制类/对象递归



我有一个类。我对这个类没有影响,它来自其他地方,来自第三方。

我有自己的班级。当我更新它是一样的。但以后可能会丢失对象。

我需要复制类let在这里叫它"source"到我的类"target"。

source有struct、int和string类型的列表。

首先,我试图在没有引用的情况下得到它,它看起来很有效,但由于缺少引用,目标在那之后是空的。

(如果有人想看代码,请告诉我)。

现在我做了第二次尝试:我不确定它是否是正确的方式,我有问题复制值到正确的位置在目标,

遍历类是可行的。请帮帮我,我需要一个紧急的解决方案。并且请将Class的现有源复制到现有目标(如果项目存在于同一结构中),

请不要建议它完全不同,因为我对类源和类目标本身没有影响,我只需要复制值和子类。

这是我目前为止的代码。通过类和子类工作,有问题设置值(到正确的位置):

void refcopyObject(ref object source,ref object target,object svalue,object tvalue) 
{ 
if (source != null && target != null) 
{ 
if (source.GetType() == (typeof(string))) 
{ 
target = source; 
} 
else 
if (source.GetType() == (typeof(int))) 
{ 
target = source; 
} 
else 
if (source.GetType() == (typeof(IntPtr))) 
{ 
target = source; 
} 
else 
{ 
FieldInfo[] fifsource = source.GetType().GetFields(BindingFlags.Instance | BindingFlags.Public); // | BindingFlags.NonPublic 
FieldInfo[] fiftarget = target.GetType().GetFields(BindingFlags.Instance | BindingFlags.Public); // | BindingFlags.NonPublic 
if (fifsource.Length > 0) 
{ 
for (int i = 0; i < fifsource.Length; i++) 
{ 
if (fifsource.GetType() == fiftarget.GetType()) 
{ 
if (i < fiftarget.Length) 
{ 
object psource = source.GetType().GetFields(); 
object ptarget = target.GetType().GetFields(); 
object vsource = source.GetType().GetFields().GetValue(source); 
object vtarget = target.GetType().GetFields().GetValue(target); 
refcopyObject(ref psource, ref ptarget, vsource, vtarget); 
} 
} 
} 
} 
else 
{ 
//Unten angekommen 
copySubObject(ref source, ref target, svalue, tvalue); 
////So gehts nicht, dann wird die Referenz wieder verloren 
//FieldInfo[] fifs = svalue.GetType().GetFields(BindingFlags.Instance | BindingFlags.Public); // | BindingFlags.NonPublic 
//if (fifs.Length > 0) 
//{ 
// FieldInfo[] fift = tvalue.GetType().GetFields(BindingFlags.Instance | BindingFlags.Public); // | BindingFlags.NonPublic 
// for (int i = 0; i < fifs.Length; i++) 
// { 
// if (fifs.GetType() == fift.GetType()) 
// { 
// if (i < fift.Length) 
// { 
// object psource = svalue.GetType().GetFields().GetValue(svalue); 
// object ptarget = tvalue.GetType().GetFields().GetValue(tvalue); 
// if (ptarget == null) 
// { 
// //Ganz unten angekommen, Problem bei Listen 
// if (psource.GetType() == (typeof(string))) 
// { 
// tvalue.GetType().GetFields().SetValue(tvalue,psource); 
// } 
// if (psource.GetType() == (typeof(int))) 
// { 
// tvalue.GetType().GetFields().SetValue(tvalue, psource); 
// } 
// } 
// else 
// { 
// refcopyObject(ref psource, ref ptarget, null, null); 
// } 
// } 
// } 
// } 
//} 
} 
} 
} 
}

我想问题是从注释开始的地方开始的。我在那部分得到了一个结构体或列表,它包含字符串或整型…

非常感谢!

快速回复

获得可序列化对象的深度副本的最简单方法是将其序列化到MemoryStream,然后将其反序列化回一个新对象:

public static T DeepCopy<T>(T other)
{
   using (MemoryStream ms = new MemoryStream())
   {
       BinaryFormatter formatter = new BinaryFormatter();
       formatter.Serialize(ms, other);
       ms.Position = 0;
       return (T)formatter.Deserialize(ms);
   }
}

请注意,这需要您的源类型标记为[Serializable]属性,并且由于您无法访问该代码,因此它不依赖于您:

[Serializable]
public class MyClass 
{
     ...
}
<<p> Non-serializable类/strong>

有一些解决方案使用反射来获得深度拷贝(如CodeProject: c#中对象的深度拷贝),尽管测试它们是否正确处理循环引用很重要。如果您的对象不引用自己(直接或间接),您可以尝试这种方法。

最新更新