使用子类递归复制类/对象

发布于 2024-11-05 03:32:06 字数 2763 浏览 0 评论 0原文

我有课。我对 thios 类没有影响,它来自其他地方,来自第 3 方。

我有自己的班级。我更新的时候也是这样。但稍后它可能会丢失对象。

我需要将 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); 
// } 
// } 
// } 
// } 
//} 
} 
} 
} 
}

我想问题是从注释开始的地方开始的。我在该部分找到了一个结构或列表,其中包含字符串或整数...

非常感谢!

快速回复

I have a class. I have no influence on thios class, it is coming from somewhere else, from a 3rd party.

I have my own class. When I update it is the same. But it might have missing objects later.

I need to copy Class lets calls it here "source" to my class "target".

source has structs, lists with ints and strings.

First I tried to get down trough it without Reference, it looked like it worked, but the target was empty after that because of the missing reference.

(if anybody wants to see that code let me know).

Now I made a 2nd attempt now: I am not sure if it is the right way, I am having problems copying the values to the right place in the target,

stepping through the class works. Please help me out I need an urgent solution. And please with copying Class existing source to existing target (if the items exisits there in the same struct),

please no suggestions to it totally different because I have no influence on Class source, and Class Target itself, I just need to copy values and subclasses.

Here my code so far. Working through class and subclasses works, have problems setting the values (to the right place):

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); 
// } 
// } 
// } 
// } 
//} 
} 
} 
} 
}

I guess the problems start where the comments start. I got to a struct or list at that part, which contain strings or int...

Thanks a lot!

Quick Reply

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

恏ㄋ傷疤忘ㄋ疼 2024-11-12 03:32:06

好吧,获取可序列化对象的深层副本的最简单方法是将其序列化为 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);
   }
}

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

[Serializable]
public class MyClass 
{
     ...
}

不可序列化类

有一些解决方案使用反思得到一个深层复制(例如 CodeProject:C# 中对象的深层复制),尽管它很重要测试它们是否正确处理循环引用。如果您的对象不引用自身(直接或间接),您可以尝试这种方法。

Well, the simplest way to get a deep copy of a serializable object is to serialize it to a MemoryStream, and then deserialize it back to a new object:

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);
   }
}

Note that this requires your source type to be marked with the [Serializable] attribute, and since you don't have access to that code, it doesn't depend on you:

[Serializable]
public class MyClass 
{
     ...
}

Non-serializable classes

There are some solutions which use Reflection to get a deep copy (like CodeProject: Deep copy of objects in C#), although it's important to test if they handle circular references properly. If your object doesn't reference itself (directly or indirectly), you may try this approach.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文