简体   繁体   English

在通用Windows应用程序(c#)中复制对象

[英]Copy an object in universal Windows Application (c#)

In want to add a copy of an object to a List in a universal Windows Application. 要在通用Windows应用程序中将对象的副本添加到列表中。 I tried several ways and found IClonable,BinaryFormatter,IFormatter which are all not available in WinRT applications. 我尝试了几种方法,发现Winlon应用程序中不提供IClonable,BinaryFormatter,IFormatter。 Please advice me a suitable way to copy a object to a new object. 请给我建议将对象复制到新对象的合适方法。 Find my code below. 在下面找到我的代码。

foreach (var ctrls in _listctrldata)
{
    for (int index = 0; index < ctrls.Controls.Control.Count; )
     {
        listofcontrolvalues.Add(ctrls.Controls.Control[index]);
        index++;
     }

   SetControlvalues(null, _vcontainer, listofcontrolvalues);

 //_vcontainer changes everytime the loop rotates and Should create a copy of  _vcontainer here//  

  VerticalContainer vcont = new VerticalContainer ();
  vcont = _vcontainer;

 _listcontrols.Add(vcont);

}

I don't know what the class VerticalContainer is. 我不知道什么是VerticalContainer类。

If it is a .NET class defined by you self, you can just defined a clone method to create a new object and copy all of the build-in data type fields. 如果它是您自己定义的.NET类,则可以定义一个克隆方法来创建一个新对象并复制所有内置数据类型字段。 If it is a Windows Runtime class, I'm afraid you cannot easily copy it because there are some internal and private data members you cannot access. 如果它是Windows Runtime类,恐怕您不能轻松地复制它,因为有些内部和私有数据成员无法访问。

In your code. 在您的代码中。 VerticalContainer vcont = new VerticalContainer (); VerticalContainer vcont =新的VerticalContainer(); vcont = _vcontainer; vcont = _vcontainer;

You only changed the vcont reference from new created object to the old _vcontainer, this will not work, you need to copy all of fields from _vcontainer to vcont one by one. 您仅将vcont引用从新创建的对象更改为旧的_vcontainer,这将不起作用,您需要将所有字段从_vcontainer一次复制到vcont。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM