Maksym Shymchenko
Maksym Shymchenko

Reputation: 23

c# How I can copy all values from first class to second

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

For example of classes:

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)

Upvotes: 0

Views: 114

Answers (1)

SValley_Developer
SValley_Developer

Reputation: 74

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; }
}

Upvotes: 4

Related Questions