utilizar el constructor que toma un diccionario. Vea este ejemplo
var dict = new Dictionary<string, string>();
dict.Add("SO", "StackOverflow");
var secondDict = new Dictionary<string, string>(dict);
dict = null;
Console.WriteLine(secondDict["SO"]);
Y solo por diversión .. ¡Puede usar LINQ! Que es un enfoque un poco más genérico.
var secondDict = (from x in dict
select x).ToDictionary(x => x.Key, x => x.Value);
Editar
Esto debería funcionar bien con los tipos de referencia, He intentado lo siguiente:
internal class User
{
public int Id { get; set; }
public string Name { get; set; }
public User Parent { get; set; }
}
Y el código modificado de arriba
var dict = new Dictionary<string, User>();
dict.Add("First", new User
{ Id = 1, Name = "Filip Ekberg", Parent = null });
dict.Add("Second", new User
{ Id = 2, Name = "Test test", Parent = dict["First"] });
var secondDict = (from x in dict
select x).ToDictionary(x => x.Key, x => x.Value);
dict.Clear();
dict = null;
Console.WriteLine(secondDict["First"].Name);
que emite " Filip Ekberg ".
Mire en http://stackoverflow.com/questions/139592/what-is-the-best-way-to-clone-deep-copy-a-net-generic-dictionarystring-t – sashaeve
Vi eso pero yo no me importa la copia profunda Una copia superficial clonará. También hay una segunda parte de mi pregunta que es: ¿Es la razón por la que no hay clon debido a la dificultad que presentan los genéricos? – uriDium