复制类(我想创建类返回完整实体) [英] copy class (I want to create class return full entity )

查看:50
本文介绍了复制类(我想创建类返回完整实体)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在c#中创建与实体框架连接到SQl服务器的应用程序

应用程序分发

firs层是包含实体框架模型的data_access层

下一层是包含应用程序的逻辑,包含所有操作函数插入选择更新

最后一层包含表单表示层

i想要添加类复制一个来自data_access的实体并在演示文稿中调用它

框架中的每个实体都与其他实体连接

类是这样的



I create application in c# that connected to SQl server with entity framework
the application is distributed
firs layer is data_access layer that contain the Entity framework model
next layer is contain logic of the application that contains all operation functions insert select update
last layer contains the forms presentation layer
i want to add class copy an entity from data_access And call it in presentation
every entity in framework is connected with other entities
the class is like this

namespace DataAccess
{
    using System;
    using System.Collections.Generic;

    public partial class tbl_Customers
    {
        public tbl_Customers()
        {
            this.tbl_Orders = new HashSet<tbl_Orders>();
        }

        public int cust_ID { get; set; }
        public string cust_Name { get; set; }
        public string cust_Phone { get; set; }
        public string cust_Address { get; set; }

        public virtual ICollection<tbl_Orders> tbl_Orders { get; set; }
    }
}







i想要创建类返回相同的值




i want t create class return same values

推荐答案

像复制一个类这样的概念毫无意义。您可以复制类,对象的实例,而不是类本身。您所需要的也称为克隆



克隆和对象最简单的方法是调用 object.MemberwiseClone 在你的实例上。请注意,将创建对象的浅拷贝。这对于您的字符串成员来说不是问题,因为字符串,作为引用对象,模仿值语义并使用 intern pool ,但是您的集合(浅层副本)将在源对象与其副本之间创建依赖关系例如,当你在一个集合中添加元素时,它会将它们添加到副本的集合中,因为它们共享相同的集合对象,保留对同一对象的两个不同引用。



要创建独立的集合成员,您需要克隆它:创建一个全新的集合并独立复制元素。它是原始对象的完整深拷贝吗?它取决于集合的元素类型。如果 tbl_Orders 是引用类型,则将共享元素。如果你想在所有情况下都有深度克隆,你需要解决一个更难的问题:创建一个深层克隆的通用机制。例如,您可以创建一个用于深度克隆的界面,但是您需要要求参与对象图的所有类都支持它;此外,您需要在所有参与的类中实现此接口。换句话说,这将是一项相当大的工作,需要思考和设计。



请参阅:

http://en.wikipedia.org/wiki/Shallow_copy#Shallow_copy [ ^ ],

http://msdn.microsoft.com/en-us/library/system.object.memberwiseclone.aspx [ ^ ],

http:// social.msdn.microsoft.com/Forums/vstudio/en-US/1dc7cc60-fc62-43d1-b9bd-2666ceef043d/deep-copy-vs-shallow-copy [ ^ ]。



-SA
The notion like "copy a class" makes no sense. You can copy the instance of the class, object, not a class itself. What you need is also called cloning.

The simplest way to clone and object is to call object.MemberwiseClone on your instance. Note that is will create a shallow copy of the object. This is not a problem for your string members, because strings, being reference objects, mimic value semantics and use intern pool, but your collection, being shallow copies, will create a dependency between source object and its copy: when, for example, you add elements in one collection, it will add them in the collection of a copy, because they share the same collection object, keeping two different references to the same object.

To create independent collection members, you would need to clone it: create a brand new one and copy elements independently. Would it be a full deep copy of original object? It depend on the element type of the collection. If tbl_Orders is a reference type, the elements will be shared. If you want to have deep clones in all cases, you would need to solve a more difficult problem: create an universal mechanism of deep cloning. For example, you could create an interface for deep cloning, but then you would need to require all classes participating in your object graph to support it; besides, you would need to implement this interface in all participating classes. In other words, it would be a considerable work, something to think at and design thoroughly.

Please see:
http://en.wikipedia.org/wiki/Shallow_copy#Shallow_copy[^],
http://msdn.microsoft.com/en-us/library/system.object.memberwiseclone.aspx[^],
http://social.msdn.microsoft.com/Forums/vstudio/en-US/1dc7cc60-fc62-43d1-b9bd-2666ceef043d/deep-copy-vs-shallow-copy[^].

—SA


这篇关于复制类(我想创建类返回完整实体)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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