C#的memcpy等效 [英] C# memcpy equivalent

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

问题描述

我有相同类型的2个对象,我想以浅拷贝一个状态到另一个。在C ++中,我有memcpy的这是伟大的。我如何能做到这一点在C#中?在MemberwiseClone()是不够的,因为它创建和放大器;返回一个新的对象,我想复制到现有的对象。我想使用反射,但我恐怕将是生产代码太慢了。我也想过使用.net序列化程序之一,但我认为他们还创建对象,而不是设置一个现有的

I have 2 objects from the same type and i would like to shallow copy one state to the other. In C++ i have memcpy which is great. How can i do it in C#? The MemberwiseClone() is not good enough because it creates & returns a new object and i like to copy to an existing object. I thought of using reflection but i'm afraid it will be too slow for production code. I also thought of using one of the .Net serializers but i think they also create object rather than setting an existing one.

我的使用案例:

我有一个模板对象(类不结构)需要由它的其中一个实例(对象所做的这个模板的)

I have a template object (class not struct) which needs to be updated by one of its instances (objects made of this template)

任何想法?

谢谢,

阿迪巴尔达

推荐答案

关于您澄清:
按我的理解,你有N个对象,每个人都有一个(直接)参考模板目的。你想要写回模板,以便所有对象看到这些修改

[edit] regarding your clarification: As I understand, you have N objects, each has a (direct) reference to the template object. You want to write back to the template so all objects "see" these changes.

建议:imlement模板经纪人

Suggestion: imlement a template broker.

class TemplateProvider
{
   public MyData Template { get; set; }
}



而不是通过模板,通过模板提供商的对象。

Instead of passing the template, pass the template provider to the objects.

要simplyfy在组件的语法,您可以添加(私营/内部?)属性。

to simplyfy the syntax in the components, you can add a (private/internal?) property

MyData Template { get { return m_templateProvider.Template; } }
void UpdateTemplate() { m_templateProvider.Template = 
                            (MyData) this.MemberwiseClone(); }



模板提供商还简化了在多线程情况下锁定。

The template provider also simplifies locking in multithreaded scenarios.


在短,没办法,除非你自己做。但是如果你无论如何覆盖所有的属性为什么不创建一个新的对象?

In short, no way unless you do it yourself. But why not create a new object if you override all properties anyway?

存储器复制和类似的低级别的结构不支持,因为他们破坏了环境做出保证。

memcopy and similar low level constructs are not supported since they undermine guarantees made by the environment.

一个浅拷贝的结构是由分配的。对于类, MemberwiseClone 是做到这一点的方法 - 但正如你说,创建一个新的对象。

A shallow copy for structs is made by assignment. For classes, MemberwiseClone is the method to do that - but as you say that creates a new object.

有在该方式没有内置,并且因为它可能会中断封装它应小心无论如何使用。

There is no built in way for that, and as it potentially breaks encapsulation it should be used with care anyway.

您可以建立使用反射一般的套路,而是它是否有效与否取决于类本身。是的,TI将comparedly缓慢。

You could build a generic routine using reflection, but whether it works or not depends on the class itself. And yes, ti will be comparedly slow.

剩下的就是通过自定义接口支持它。可以提供一个通用的浅复制例程检查接口,并使用该,而当它不属于回反射。这使得一般可用的功能,而且可以优化其性能事项后的类。

What's left is supporting it by a custom interface. You can provide a generic "Shallow Copy" routine that checks for the interface and uses that, and falls back to reflection when it doesn't. This makes the functionality available generally, and you can optimize the classes for which performance matters later.

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

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