0

I have a class that contains a list. I want to copy that list to another object that contains the same type and amount of attributes.

        List<CinemaUnitSchema> cinemaUnitSchemas = new List<CinemaUnitSchema>();
        foreach (CinemaUnit cinemaUnits in scenario.CinemaUnits)
        {
            cinemaUnitSchemas.Add(new CinemaUnitSchema
            {
                Name = cinemaUnits.Name,
                AttendantPoints = cinemaUnits.AttendantPoints,
                ShowPoints = cinemaUnits.ShowPoints
            });                

        }
        scenarioSchema.CinemaUnits.AddRange(CinemaUnitSchemas);

However, I'm receiving an error in this line of code;

AttendantPoints = cinemaUnits.AttendantPoints

The error I'm receiving is:

"Cannot implicitly convert type 'System.Collections.Generic.List < MyApp.Models.AttendantPoint >' to 'System.Collections.Generic.List < MyApp.Schemas.AttendantPointSchema >'."

Class of CinemaUnit is:

public class CinemaUnit
{
    public string Name { get; set; }    
    public List<AttendantPoint> AttendantPoints { get; set; }
    public bool ShowPoints { get; set; }
}

The Class of CinemaUnitSchema is:

public class CinemaUnitSchema
{
    public string Name { get; set; }    
    public List<AttendantPoint> AttendantPoints { get; set; }
    public bool ShowPoints { get; set; }
}

Solution Intended

Add in each iteration the respective list to the new object.

Thanks,

8
  • 1
    scenarioSchema.CinemaUnits.AddRange(CinemaUnitSchemas); - you trying add list of one type to list of different type Commented Sep 20, 2017 at 18:29
  • And how do I fix it? Commented Sep 20, 2017 at 18:32
  • Both list will contain exactly the same amount of data, it's literally a copy of the list. Commented Sep 20, 2017 at 18:33
  • You cannot add objects of different types in same collection. This a benefit of strong typed languages (c#) Commented Sep 20, 2017 at 18:33
  • I'm creating a copy of a class, and naming it differently. There must be a way. See the classes? They contain same attributes. Commented Sep 20, 2017 at 18:33

3 Answers 3

2

You can write a Copy method that makes a shallow copy using reflection.

void Copy(object from, object to)
{
    var dict = to.GetType().GetProperties().ToDictionary(p => p.Name, p => p);
    foreach(var p in from.GetType().GetProperties())
    {
        dict[p.Name].SetValue(to, p.GetValue(from,null), null);
    }
}
Sign up to request clarification or add additional context in comments.

4 Comments

Thank you Eser. The method is giving me an error in p.GetValue() indicating that 'No overload method 'GetValue' takes 1 argument'.
@Codinghierarchy p.GetValue(from,null) I updated the answer
OP already doing this but manually(which is more performant then reading properties through reflection on every copy ;)). Main problem is that OP try assign collection of one type to another. And expects it should work when both types have properties with same name - which is not a case in the C#.
I confirm what Fabio just said. The suggested solution, although highly performance wise, it doesn't solve my problem.
0

What you actually need it's a way to convert AttendantPoint to AttendantPointSchema.

Solution 1: You can use AutoMapper framework to do it.

Solution 2: You can write generic converter like @Eser suggested.

Solution 3: You can crate converter manually for each class using extension methods, implicit or explicit operators or just write helper class with static functions.

Comments

0

Not sure if this is the problem, but it is probably a good bet that it is. You are using a foreach statement with the camel case cinemaUnits but when you are trying to copy the fields you are using the title case CinemaUnits instead of the variable with camel case.

1 Comment

Thank you for your feedback, I just edited my question. I copied and pasted it the wrong way. That is not the error causing issue. However, thanks for bringing it up.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.