复制一个类,C# [英] copy a class, C#

查看:338
本文介绍了复制一个类,C#的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

有没有办法在C#中复制一个类?像VAR欺骗= MyClass的(原件)。

Is there a way to copy a class in C#? Something like var dupe = MyClass(original).

推荐答案

并非所有的类都具有此功能。也许,如果一个类呢,它提供了一个克隆方法。为了帮助实施自己的类这种方法有一个在 System.Object的定义的 MemberwiseClone 保护的方法,使一个浅拷贝当前实例(即场被复制;如果是引用类型,参考将指向原来的位置)

Not all classes have this functionality. Probably, if a class does, it provides a Clone method. To help implement that method for your own classes there's a MemberwiseClone protected method defined in System.Object that makes a shallow copy of the current instance (i.e. fields are copied; if they are reference types, the reference will point to the original location).

这篇关于复制一个类,C#的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆