Tag: 克隆

.net中DataContract属性和Serializable属性之间的区别

我正在尝试使用以下方法创建对象的深层克隆。 public static T DeepClone(this T target) { using (MemoryStream stream = new MemoryStream()) { BinaryFormatter formatter = new BinaryFormatter(); formatter.Serialize(stream, target); stream.Position = 0; return (T)formatter.Deserialize(stream); } } 该方法需要一个Serialized对象,即一个具有“Serializable”属性的类的对象。 我有一个类,它具有属性“DataContract”,但该方法不使用此属性。 我认为“DataContract”也是一种序列化器,但可能与“Serializable”不同。 有人可以给我两者的区别吗? 另外请告诉我是否可以创建一个只有1个属性的对象的深度克隆,它可以完成“DataContract”和“Serializable”属性的工作,也可能是创建深度克隆的不同方式? 请帮忙!

绑定没有克隆方法,是一种有效的复制方法

我希望复制一个绑定,这是我可以在其上设置不同的源属性而不影响原始绑定。 这只是将新绑定的所有属性设置为与旧的相同的情况吗?

无法访问受保护的成员’object.MemberwiseClone()’

我正在尝试在我的自定义类上使用.MemberwiseClone() ,但它会抛出此错误: Cannot access protected member ‘object.MemberwiseClone()’ via a qualifier of type ‘BLBGameBase_V2.Enemy’; the qualifier must be of type ‘BLBGameBase_V2.GameBase’ (or derived from it) 这是什么意思? 或者更好的是,我如何克隆Enemy类?

C#reflection索引属性

我正在使用reflection编写克隆方法。 如何使用reflection检测属性是否为索引属性? 例如: public string[] Items { get; set; } 到目前为止我的方法: public static T Clone(T from, List propertiesToIgnore) where T : new() { T to = new T(); Type myType = from.GetType(); PropertyInfo[] myProperties = myType.GetProperties(); for (int i = 0; i < myProperties.Length; i++) { if (myProperties[i].CanWrite && !propertiesToIgnore.Contains(myProperties[i].Name)) { myProperties[i].SetValue(to,myProperties[i].GetValue(from,null),null); } } return […]

克隆不同对象属性的最佳方法

我有一个MVC3应用程序需要将视图模型与数据库模型同步。 我发现自己写了太多代码来在不同对象之间来回复制属性。 我避免了这种情况,我可以简单地对数据模型进行子类化,但在其他时候,我发现它太过限制了。 我在Object上开发了一些扩展方法,以支持具有相似名称的属性的浅层克隆,并且这种方法运行得相当好。 但是,我想知道是否有更有效的方法来完成同样的事情。 所以我想这是要求同行评审和选项来改进这个代码。 更新:我发现明确处理相关表格更好。 对IsVirtual进行测试可以防止在克隆过程中无意中影响关系。 请参阅更新的CloneMatching方法。 其他人明确说明要更新或排除的属性。 public static class CustomExtensions { public static T CloneMatching(this T target, S source) where T : class where S : class { if (source == null) { return target; } Type sourceType = typeof(S); Type targetType = typeof(T); BindingFlags flags = BindingFlags.IgnoreCase | BindingFlags.Public | […]

EF中的可序列化类和动态代理 – 如何?

在[之前的post]中 ,我被设置为必须克隆我的实体的路径。 我尝试使用[codeproject]中的序列化方法。 因为这些类是由Entity Framework生成的,所以我在自定义.cs中单独标记它们,如下所示: [Serializable] public partial class Claims { } 但是,当检查时(在克隆方法中): if (Object.ReferenceEquals(source, null)) { 被击中,我收到错误: System.ArgumentException was unhandled by user code Message=The type must be serializable. Parameter name: source Source=Web ParamName=source StackTrace: at .Web.Cloner.Clone[T](T source) in C:\Users\.\Documents\Visual Studio 2010\Projects\.\Website\Extensions.Object.cs:line 49 at .Web.Models.Employer..ctor(User u) in C:\Users\.\Documents\Visual Studio 2010\Projects\.\Website\Models\EF.Custom.cs:line 121 at .Web.Controllers.AuthController.Register(String Company, String […]

有没有更好的方法在C#中创建深度和浅层克隆?

我一直在为项目创建对象,有些实例我必须为这些对象创建一个深层副本我已经想到了使用C#的内置函数,即MemberwiseClone()。 困扰我的问题是每当我创建一个新类时,我都必须编写一个类似下面的代码的函数来进行浅拷贝。有人请帮助我改进这部分并给我一个更好的浅拷贝比第二行代码。 谢谢 :) SHALLOW COPY: public static RoomType CreateTwin(RoomType roomType) { return (roomType.MemberwiseClone() as RoomType); } 深度复制: public static T CreateDeepClone(T source) { if (!typeof(T).IsSerializable) { throw new ArgumentException(“The type must be serializable.”, “source”); } if (Object.ReferenceEquals(source, null)) { return default(T); } IFormatter formatter = new BinaryFormatter(); Stream stream = new MemoryStream(); using (stream) […]