0

I'm attempting to copy a List<Windows.UI.Xaml.Controls.Maps.MapElement> but have so far only managed to copy the reference. Can anyone offer a way to do this without creating the original MapIcon object again.

I now understand why the methods i've attempted don't work, but i can't find a way around it.

public void MyTestFunction(BasicGeoposition nPosition) 
{

List<MapElement> MyLandmarks = new List<MapElement>();

Geopoint nPoint = new Geopoint(nPosition, AltitudeReferenceSystem.Ellipsoid);

var needleIcon = new MapIcon    //has base class MapElement
{
    Location = nPoint,                      
    NormalizedAnchorPoint = new Windows.Foundation.Point(0.5, 0.5),                    
    ZIndex = 0,                    
    Title = "Point 1"
};
            
MyLandmarks.Add(needleIcon);

// Copy Mylandmarks by value 
// Attempt 1 - copies reference
// copyOfMapElements = new List<MapElement>();
// copyOfMapElements = MyLandmarks;
//
// Attempt 2 - copies reference

copyOfMapElements = new List<MapElement>(MyLandmarks);
}
AlexS
  • 510
  • 2
  • 7
  • 23

2 Answers2

0

You can use LINQ to do that:

copyOfMapElements = MyLandmarks.Select(l => new MapIcon{ Location = l.Location,                      
    NormalizedAnchorPoint = l.NormalizedAnchorPoint,                    
    ZIndex = l.ZIndex,                    
    Title = l.Title }).ToList();

Update: The above solution assumes that all list elements of type MapIcon only, but if you need more generic solution to handle all MapElement derived types then you can used serialization or reflection. Check this answer for JSON serialization: https://stackoverflow.com/a/55831822/4518630

copyOfMapElements = JsonSerializer.Deserialize<List<MapElement>>(JsonSerializer.Serialize(list));
Fadi Hania
  • 705
  • 4
  • 11
  • Thanks but its not quite working, i get error CS1061 on l.Location, l.NormalizedAnchorPoint and l.Title. i.e 'MapElement' does not contain a definition for 'Location' and no accessible extension method 'Location' accepting a first argument of type 'MapElement' could be found (are you missing a using directive or an assembly reference?). – AlexS Aug 12 '22 at 22:12
  • You can either use Serialization or Reflection for deep copying all your objects of different `MapElement` or derived types – Fadi Hania Aug 12 '22 at 22:36
0

Simple answer with only MapIcon items in the list:

copyOfMapElements = MyLandmarks.ConvertAll(lm => new MapIcon {
    Location = lm.Location,
    NormalizedAnchorPoint = lm.NormalizedAnchorPoint,
    ZIndex = lm.ZIndex,
    Title = lm.Title
});

But since MapIcon is derived from MapElement class and MyLandmarks list holds MapElement and not MapIcon which has its own set of properties you cannot use the example above (kinda weird there's no ICloneable interface implemented for these classes), so I'd probably check for the type of the element and create new instance accordingly.

Daniel
  • 202
  • 1
  • 3
  • ah yes that's the route i was just starting to think about using MapElement.GetType() == typeof(MapIcon) etc. I was hoping there would be a trick i'm just not seeing. – AlexS Aug 12 '22 at 22:21
  • i should add i want MyLandmarks to hold MapElement because i want to deal with different types of MapElement not just MapIcon. Otherwise i'm sure your solution would be fine! – AlexS Aug 12 '22 at 22:24