Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Can't clone dictionary #34

Open
kakkamstrup opened this issue Jun 26, 2023 · 5 comments
Open

Can't clone dictionary #34

kakkamstrup opened this issue Jun 26, 2023 · 5 comments

Comments

@kakkamstrup
Copy link

Here is a piece of code to recreate the bug, I hope.

using System.Collections.Generic;
using System.Linq;
using Force.DeepCloner;

namespace DeepClonerBug
{
internal class Program
{
static void Main(string[] args)
{
var org = new Dictionary<Location, Location[]>
{
{ new Location { Name = "A" }, new[] { new Location { Name = "1" } } }
};

        org[org.Keys.First()].ToString();

        var clone = org.DeepClone();

        // throws System.Collections.Generic.KeyNotFoundException: 'The given key was not present in the dictionary.'
        clone[clone.Keys.First()].ToString();
    }
}

public class Location
{
    public string Name { get; set; }
}

}

@kakkamstrup
Copy link
Author

Here is another example with HashSet. I guess they have the same root cause

using System;
using System.Collections.Generic;
using System.Linq;
using Force.DeepCloner;

namespace DeepClonerBug
{
internal class Program
{
static void Main(string[] args)
{
var org = new HashSet { new Location() };
if (!org.Contains(org.First())) throw new Exception();

        var clone = org.DeepClone();
        //uncomment to fix:  clone = new HashSet<Location>(clone.ToList());

        if (!clone.Contains(clone.First())) throw new Exception();
    }
}

public class Location
{
}

}

@force-net
Copy link
Owner

You need to override GetHashCode and Equals methods for Location class. It bad idea to add to dictionary objects without overriding these methods. It can cause issue in any situation

set.Add(new Location { Name = "A" });
set.Contains(new Location { Name = "A" }) // false

I know this issue but I do not found ways how to correctly bypass it. I can make some hotfixes for Dictionary and HashSet, but there are lot of another classes, with using these methods (e.g. ConcurrentDictionary, or LINQ method Distinct which creates specific internal HashSet).

@kakkamstrup
Copy link
Author

OK, thank you for your reply. I can make it work with overriding GetHashCode and Equals.

Maybe this is not the right place to ask. I want to clone the same object multiple times, is there a way to speed up performance for this case?

@force-net
Copy link
Owner

I want to clone the same object multiple times
Code is optimized, so there are no sense to add special method for this (it will be complex with internal state). I'll think about this. May be I found way to do this method simple and self-descriptive.

@lofcz
Copy link

lofcz commented Jan 8, 2025

Cloning of dictionaries, including read-only, concurrent, sets, ordered and other specializations is fixed in FastCloner.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants