Creating a generic Clone() method for dictionaries

I wanted to create a Clone method for dictionaries which returns the same type as the Dictionary on which the method was called:

This would be quite easy if we would do an extension method for every type of dictionary, but I wanted to do this for all IDictionary implementations. Here is my solution:

Since cloning works by creating a new instance with the source dictionary as constructor parameter, we have to use Activator.CreateInstance to create a new instance.

Comments

Popular posts from this blog

Debugging Lua scripts in VS Code using MoonSharp

Testing the response body of middleware in ASP.NET Core

Make ‘dotnet ef‘ work with EF Core 2.0