0

I have following code on c# Have two objects of class. First class it's ancestor of second. And I just want copy all values to second class. How I can do it? If I don't want to create constructor and others function in class.

ItemsInformationWithWarehouses TowarIMagazyn = new ItemsInformationWithWarehouses();

            TowarIMagazyn.magid1 = i.magid1;
            TowarIMagazyn.magid2 = i.magid2;
            TowarIMagazyn.dokmsid1 = i.dokmsid1;
            TowarIMagazyn.dokmsid2 = i.dokmsid2;
            TowarIMagazyn.Dostawa = i.Dostawa;
            TowarIMagazyn.Symbol = i.Symbol;
            TowarIMagazyn.Towar = i.Towar;
            TowarIMagazyn.Partia = i.Partia;
            TowarIMagazyn.PartiaE = i.PartiaE;
            TowarIMagazyn.SSCC = i.SSCC;
            TowarIMagazyn.PartiaO = i.PartiaO;
            TowarIMagazyn.Pozostalo = i.Pozostalo;
            TowarIMagazyn.JM = i.JM;
            TowarIMagazyn.sWc1 = i.sWc1;
            TowarIMagazyn.sWc2 = i.sWc2;
            TowarIMagazyn.sWc3 = i.sWc3;
            TowarIMagazyn.sWc4 = i.sWc4;
            TowarIMagazyn.sWc5 = i.sWc5;
            TowarIMagazyn.DataWstawienia = i.DataWstawienia;
            TowarIMagazyn.WyprodukowanoDla = i.WyprodukowanoDla;
            TowarIMagazyn.IndeksOdbiorcy = i.IndeksOdbiorcy;
            TowarIMagazyn.IndeksDostawcy = i.IndeksDostawcy;
public class ItemsInformation 
{
    public int magid1 { get; set; } 
    public int magid2 { get; set; }
    public int dokmsid1 { get; set; }
    public int dokmsid2 { get; set; }
    public string Dostawa { get; set; }
    public string Symbol { get; set; }
    public string Towar { get; set; }
    public string Partia { get; set; }
    public string PartiaE { get; set; }
    public string SSCC { get; set; }
    public string PartiaO { get; set; }
    public string Pozostalo { get; set; }
    public string JM { get; set; }
    public string sWc1 { get; set; }
    public string sWc2 { get; set; }
    public string sWc3 { get; set; }
    public string sWc4 { get; set; }
    public string sWc5 { get; set; }
    public string DataWstawienia { get; set; }
    public string WyprodukowanoDla { get; set; }
    public string IndeksOdbiorcy { get; set; }
    public string IndeksDostawcy { get; set; }

}



public class ItemsInformationWithWarehouses : ItemsInformation
{
    List<Magazyny> Magazyny = new List<Magazyny>();
}


public class Magazyny
{
    public string SymbolMagazyn { get; set; }
    public string fromMagazyn { get; set; }
    public int fromMagazynMagId1 { get; set; }
    public int fromMagazynMagId2 { get; set; }
    public int MagazynMagId1 { get; set; }
    public int MagazymMagId2 { get; set; }
}

I want do like this CopyValueOfElementsFromFirstToSecond(TowarIMagazyn,i)

  • It's really not clear what you're asking. It appears you have the second and want to copy it to the first. – Enigmativity Nov 08 '22 at 07:46
  • Maybe this answer can help: [link](https://stackoverflow.com/questions/78536/deep-cloning-objects) – Alessandro Nov 08 '22 at 07:49
  • If you are fine with creating a new copy, you can call `MemberwiseClone` to create a copy. However, this should only be used as is if all fields are either value types or string, as for reference types it will only copy the reference to the same object. – ckuri Nov 08 '22 at 07:55

1 Answers1

4

AutoMapper is a great option for mapping one object to another. You can customize you mappings as well if you have some inconsistencies between names in classes.

You can add the AutoMapper package with NuGet

PM> Install-Package AutoMapper

You can use AutoMapper like this. Note, you should instantiate the configuration once, as a singleton, per application.

using AutoMapper;
                    
public class Program
{
    public static void Main()
    {
        var config = new MapperConfiguration(cfg => cfg.CreateMap<Wibble, Wobble>());
        var mapper = new Mapper(config);
        var wobble = mapper.Map<Wobble>(
            new Wibble
            {
                Foo = 1,
                Bar = "s"
            });
    }
}

public class Wibble
{
    public int Foo { get; set; }
    public string Bar { get; set; }
}

public class Wobble
{
    public int Foo { get; set; }
    public string Bar { get; set; }
}
Jodrell
  • 34,946
  • 5
  • 87
  • 124