简体   繁体   English

是否有与Delphi的Assign / AssignTo相同的.NET语法?

[英]Is there a .NET equivalent syntax to Delphi's Assign/AssignTo?

Delphi has a standard mechanism where one object can be copied from another, paraphrased here as: Delphi有一个标准机制,一个对象可以从另一个对象复制,在这里解释为:

class Persistent 
{
    protected AssignError(Persistent source)
    {
       throw new ConvertError("Cannot assign a {0} to a {1}", source.GetType().Name, this.GetType().Name);
    }

    protected virtual AssignTo(Persistent destination)
    {
        destination.AssignError(this);
    }

    public virtual Assign(Persistent source)
    {
       source.AssignTo(this);
    }
}

Does the .NET FCL has a canonical syntax of copying objects between each other? .NET FCL是否具有在彼此之间复制对象的规范语法?

eg: 例如:

interface IPersistent
{
   public virtual Assign(Object source);
}

public class User : IPersistent
{
    private Image avatarThumbnail;
    private String name;

    public override Assign(Object source)
    {
       if (source is User)
       {
          this.avatarThumbnail = source.avatarThumbnail;
          this.name = source.Name;
       }
       else if (source is Image)
           avatarThumbnail = (Image)source;
       else if (source is DirectoryEntry)
           name = ((DirectoryEntry) source).Firstname + " " + ((DirectoryEntry) source).Lastname;
       else
           throw new AssignError("Assign from {0}", source.GetType());
    }
}

Yes i just invented a standard IPersistent interface; 是的,我刚刚发明了一个标准的IPersistent接口; but is there already a mechanism to copy objects between each other? 但是,是否已经存在一种在彼此之间复制对象的机制?

Update 更新

Note : i am talking about the opposite of cloning an object. 注意 :我说的是克隆对象的反面

User originalUser = new User();
User theClone = originalUser.Clone();
theClone.Lastname = "Guyer";

originalUser.Assign(theClone);

Or, i don't even need to have a clone at all: 或者,我根本不需要克隆:

User u = new User();
u.Assign(salesOrder); //copy name/e-mail from the sales order

or 要么

SalesOrder s = new SalesOrder();
s.SalesOrderDate = DateTime.Now;
User customer = new Customer("Kirsten");
s.Assign(user); //copy sold-to/ship-to information from the user

The .NET framework has the ICloneable interface: .NET框架具有ICloneable接口:

Supports cloning, which creates a new instance of a class with the same value as an existing instance. 支持克隆,它创建一个具有与现有实例相同值的类的新实例。

And: 和:

The ICloneable interface contains one member, Clone , which is intended to support cloning beyond that supplied by MemberwiseClone . ICloneable接口包含一个成员Clone ,用于支持MemberwiseClone提供的克隆。 For more information about cloning, deep versus shallow copies, and examples, see the Object.MemberwiseClone method. 有关克隆,深拷贝和浅拷贝以及示例的详细信息,请参阅Object.MemberwiseClone方法。


Update: 更新:

As far as I know, there is nothing equivalent to what you want (standard way to copy some values between different objects) in the BCL. 据我所知,在BCL中没有任何东西与你想要的东西相同(在不同对象之间复制某些值的标准方法)。

As an alternative to the standard ICloneable interface, you could perform the copy of public properties/fields using reflection. 作为标准ICloneable接口的替代方案,您可以使用反射执行公共属性/字段的副本。 Here's some code written off the top of my head for doing so: 这里有一些代码写在我的头顶:

foreach(PropertyInfo mbr in src.GetType().GetProperties())
    mbr.SetValue(dst, src.GetValue(src, BindingFlags.GetProperty, null, null, null), BindingFlags.SetProperty, null, null, null)

foreach(FieldInfo mbr in src.GetType().GetFields())
    mbr.SetValue(dst, src.GetValue(src, BindingFlags.GetField, null, null, null), BindingFlags.SetField, null, null, null)

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

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