Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 3286526
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 17, 20262026-05-17T20:23:25+00:00 2026-05-17T20:23:25+00:00

I am working in the .NET 2.0 framework. I have some code working, just

  • 0

I am working in the .NET 2.0 framework. I have some code working, just want it working a little more elegantly.

I have a need to effectively “mirror” a Dictionary object such that if we start with an object like this

Dictionary<TKey,TValue> StartDictionary;

We can Mirror it like this

Dictionary<TValue,TKey> MirroredDictionary = MirrorDictionary(StartDictionary);

And we would end up with a new dictionary with the values and keys being swapped for each KeyValuePair

Before anyone asks me why: the source dictionary is fairly large and loaded once from reflection calls when my program loads. I don’t want to run the same reflection calls a second time to load the mirrored dictionary. Creating a mirrored Dictionary and populating its values and keys the way I came up with seemed to me to be much less costly.

So being the kind of person that hates to rewrite things, I decided to write a Generic method in a helper class I have to do the Mirror using Generics.

Now mind you I’ve written simple Generic methods before for normal scalar types

Here’s what I came up with

 public static TOutDic MirrorDictionary<TInDic, TOutDic>(TInDic InDictionary)
  where TInDic : IDictionary
  where TOutDic : IDictionary
{
  Type[] KVPTypes = typeof(TInDic).GetGenericArguments();
  Type TKey = KVPTypes[0];
  Type TValue = KVPTypes[1];
  Type TDic = typeof(Dictionary<,>).MakeGenericType(TValue, TKey);
  IDictionary OutDic = (IDictionary)Activator.CreateInstance(TDic);
  foreach (DictionaryEntry DE in (IDictionary)InDictionary) OutDic.Add(DE.Value, DE.Key);
  return (TOutDic)OutDic;
}

A little bit there but it works, Loads up the Types of the Keys and Values and creates an instance of the mirrored Dictionary

Then just looping through the base DictionaryEntries of the InDictionary it adds the items to the OutDic and returns it casting it to the Type expected

Compiles just fine

Now when i go to call it I would think just like when i call a Generic method for a scalar type I could just using our code snippits above say

Dictionary<TValue,TKey> MirroredDictionary = MirrorDictionary(StartDictionary);

But that does not compile gives me

The type arguments for method MirrorDictionary(TInDic)’ cannot be inferred from the usage. Try specifying the type arguments explicitly.

So If I call it instead like this

      Dictionary<TValue, TKey> MirrorDic = MirrorDictionary<Dictionary<Tkey, TValue>, Dictionary<TValue,TKey>>(StringDic);

It compiles and works like a charm.

Now the question becomes how do I make it properly infer the Type being passed into this method when the Type being passed in and the Type being passed out are complex types like in this example?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-17T20:23:25+00:00Added an answer on May 17, 2026 at 8:23 pm

    You can make life much easier for the compiler by telling it the key and value types thus:

    public static Dictionary<TValue, TKey> MirrorDictionary<TKey, TValue>
        (Dictionary<TKey, TValue> source)
    {
        Dictionary<TValue, TKey> destination = new Dictionary<TValue, TKey>();
    
        foreach (KeyValuePair<TKey, TValue> kvp in source)
        {
            destination.Add(kvp.Value, kvp.Key);
        }
    
        return destination;
    }
    

    I don’t think you need reflection here at all.

    Sample usage:

    static void Main(string[] args)
    {
        Dictionary<int, string> source = new Dictionary<int, string>();
        source.Add(3, "foo");
        source.Add(4, "bar");
    
        DumpDic(source);
    
        DumpDic(MirrorDictionary(source));
    
        Console.ReadLine();
    
    }
    

    where DumpDic is:

    public static void DumpDic<TK, TV>(Dictionary<TK, TV> dic)
    {
        foreach (KeyValuePair<TK, TV> keyValuePair in dic)
        {
            Console.WriteLine("{0} => {1}", keyValuePair.Key, keyValuePair.Value);
        }
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

No related questions found

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.