2017-09-01 3 views
1

私が変更できない親クラスのオブジェクトがあるとします。たとえば、パラメータの長いリストを持つListBoxのインスタンスです。今、私は子クラスを作成します。C#すべてのオブジェクトパラメータを子クラスオブジェクトにコピー

class PlaylistBox : ListBox 
{ 

    void CopySettingsFrom(ListBox In) 
    { 
     //...what now? 
    } 
} 

質問 - どのように私は効率的PlaylistBoxの新しいオブジェクトにInオブジェクトから浅いコピーを作成することができますか?

+5

を、あなたはClone' '呼び出します。それ以外の場合は、コードを記述します。これには魔法はありません。 –

+0

基本クラスのパブリックプロパティとフィールド、または内部メンバーまたはプライベートメンバーのみが必要かどうかによって異なります。基本的には、コピーする単一のフィールド/プロパティごとに 'this.MyProperty = In.MyProperty'と書くだけです。彼らの多くがある場合、反射は助けになるかもしれません。 – HimBromBeere

+0

@PanagiotisKanavos 'Clone'を呼び出すコンストラクタ内では、明らかに' this'に割り当てることができないインスタンスを返すので、大いに役立ちません。とにかく、新しいインスタンスにクローンのメンバーをコピーする必要があります。 – HimBromBeere

答えて

0

あなたはここで説明して反射し、AutoMapperに基づいて3つの方法、との例反射

//Other Imports... 
    using System.Reflection; 

    public PlaylistBox(ListBox In) 
    { 
     PropertyInfo[] properties = typeof(ListBox).GetProperties(); 

     foreach (PropertyInfo p in properties) 
      if (p.CanRead && p.CanWrite) 
       p.SetMethod.Invoke(this, new object[] { p.GetMethod.Invoke(In, null) }); 
    } 
1

を使用することができます: `Listbox`は` ICloneable`を実装している場合

internal class Program 
    { 
     private static void Main(string[] args) 
     { 
      Example1(); 
      Example2(); 
      Example3(); 
     } 

     public static void Example1() 
     { 
      Console.WriteLine("This example shows using copy with reflection. Minus of this method - u have to implement FULL copy for each class or u will copy only references to object properties"); 

      //creating new parent class with some values 
      var parentClass = new ParentClass 
      { 
       Property1 = "qqq", 
       Property2 = 1, 
       ObjectProperty = new SomeClassWithObjectProperty 
       { 
        ObjectProperty = new SomeObjectClass {SomeProperty = "www"} 
       } 
      }; 

      //crating new child class and copy REFERENCES to properties 
      var childClassReflection = new ChildClassReflection(parentClass); 

      //changing properties of parent 
      parentClass.Property1 = "rrr"; 
      parentClass.Property2 = 2; 
      parentClass.ObjectProperty.ObjectProperty.SomeProperty = "eee"; 

      //we will get OLD values for VALUE types and OLD values for REFERENCE types 
      //qqq 1 WWW 
      Console.WriteLine(childClassReflection.Property1 + " " + childClassReflection.Property2 + " " + childClassReflection.ObjectProperty.ObjectProperty.SomeProperty); 
     } 

     public static void Example2() 
     { 
      Console.WriteLine(); 
      Console.WriteLine("This example shows using copy with reflection WITH FULL COPY"); 

      //creating new parent class with some values 
      var parentClass = new ParentClass 
      { 
       Property1 = "qqq", 
       Property2 = 1, 
       ObjectProperty = new SomeClassWithObjectProperty 
       { 
        ObjectProperty = new SomeObjectClass {SomeProperty = "www"} 
       } 
      }; 

      //crating new child class and copy REFERENCES to properties 
      var childClassReflection = new ChildClassReflectionWithFullCopy(parentClass); 

      //changing properties of parent 
      parentClass.Property1 = "rrr"; 
      parentClass.Property2 = 2; 
      parentClass.ObjectProperty.ObjectProperty.SomeProperty = "eee"; 

      //we will get OLD values for VALUE types and NEW values for REFERENCE types 
      //qqq 1 eee 
      Console.WriteLine(childClassReflection.Property1 + " " + childClassReflection.Property2 + " " + childClassReflection.ObjectProperty.ObjectProperty.SomeProperty); 
     } 

     public static void Example3() 
     { 
      //here i will show copy using AutoMapper 
      Console.WriteLine(); 
      Console.WriteLine("This example shows using copy with AutoMapper"); 

      //creating new parent class with some values 
      var parentClass = new ParentClass 
      { 
       Property1 = "qqq", 
       Property2 = 1, 
       ObjectProperty = new SomeClassWithObjectProperty 
       { 
        ObjectProperty = new SomeObjectClass { SomeProperty = "www" } 
       } 
      }; 

      Mapper.Initialize(cfg => cfg.CreateMap<ParentClass, ChildClassAutoMapper>());   

      //crating new child class and copy REFERENCES to properties 
      var childClassReflection = Mapper.Map<ChildClassAutoMapper>(parentClass); 

      //changing properties of parent 
      parentClass.Property1 = "rrr"; 
      parentClass.Property2 = 2; 
      parentClass.ObjectProperty.ObjectProperty.SomeProperty = "eee"; 

      //we will get OLD values for VALUE types and OLD values for REFERENCE types 
      //qqq 1 eee 
      Console.WriteLine(childClassReflection.Property1 + " " + childClassReflection.Property2 + " " + childClassReflection.ObjectProperty.ObjectProperty.SomeProperty); 
     } 
    } 

    public class ChildClassAutoMapper:ParentClass 
    {  
    } 

    public class ChildClassReflection : ParentClass 
    { 
     public ChildClassReflection(ParentClass parentClass) 
     { 
      foreach (var p in ParentProperties) 
       p.SetMethod.Invoke(this, new[] {p.GetMethod.Invoke(parentClass, null)}); 
     } 

     //do it only once for best performance 
     private static PropertyInfo[] ParentProperties { get; } = typeof(ParentClass).GetProperties().Where(c => c.CanRead && c.CanWrite).ToArray(); 
    } 

    public class ChildClassReflectionWithFullCopy : ParentClass 
    { 
     public ChildClassReflectionWithFullCopy(ParentClass parentClass) 
     { 
      var parentClassLocal = JsonConvert.DeserializeObject<ParentClass>(JsonConvert.SerializeObject(parentClass)); 
      foreach (var p in ParentProperties) 
       p.SetMethod.Invoke(this, new[] {p.GetMethod.Invoke(parentClassLocal, null)}); 
     } 

     //do it only once for best performance 
     private static PropertyInfo[] ParentProperties { get; } = typeof(ParentClass).GetProperties().Where(c => c.CanRead && c.CanWrite).ToArray(); 
    } 

    public class ParentClass 
    { 
     public string Property1 { get; set; } 
     public int Property2 { get; set; } 
     public SomeClassWithObjectProperty ObjectProperty { get; set; } 
    } 

    public class SomeClassWithObjectProperty 
    { 
     public SomeObjectClass ObjectProperty { get; set; } 
    } 

    public class SomeObjectClass 
    { 
     public string SomeProperty { get; set; } 
    } 
関連する問題