A: 

You could define the Out dictionary as an out parameter. Type inference does not look the type of the variable you're assigning to, only the types of the parameters. That's the reason this doesn't compile.

Pieter
A: 

You need to tell it what TValue and TKey are. Unless they are defined up in the signature of the method calling this code, they don't have any specific types. You need to give it something like:

Dictionary<string, int> MirroredDictionary = MirrorDictionary(StartDictionary);
Joel Etherton
This doesn't help the compiler infer the type of TOutDic, and this line gives the same compiler error.
stevemegson
@stevemegson - No, upon reading further it doesn't even come close, I was tempted to delete it, but after the downvote I wanted leave it to give someone a chance to comment.
Joel Etherton
I'll retract the downvote then, I only bothered to downvote because the single upvote made it the top answer at the time.
stevemegson
@stevemegson - the upvote should removed as well, but I'll leave it up as an example of how not to answer.
Joel Etherton
+1  A: 

Here's a 3.5 solution (you can also use it in 2.0 with VS2008 and LinqBridge)

IDictionary<TValue, TKey> MirrorDictionary<TKey, TValue>(IDictionary<TKey, TValue> dict)
{
    return dict.ToDictionary(kvp => kvp.Value, kvp => kvp.Key);
}

And a pure 2.0 solution

IDictionary<TValue, TKey> MirrorDictionary<TKey, TValue>(IDictionary<TKey, TValue> dict)
{
    Dictionary<TValue, TKey> newDict = new Dictionary<TValue, TKey>();
    foreach(KeyValuePair<TKey, TValue> kvp in dict)
    {
        newDict.Add(kvp.Value, kvp.Key);
    }
    return newDict;
}

Type inference should work fine with both solutions (as they have the same signature)

Thomas Levesque
As well as avoiding reflection, this also enforces that the input and output types are correct. There's nothing to stop you calling the original method as `MirrorDictionary<Dictionary<int,string>,Dictionary<Guid,bool>>(input)`, and it'll compile fine but blow up with an invalid cast at runtime.
stevemegson
+1  A: 

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);
    }
}
AakashM
I tried that first it would not compile for me for some reason Let me go back and retry it maybe i messed up when i was creating the method the first time. I'll repost when I've tested
TofuBug
Yep completely rushed over the method declaration and forgot to declare my types so i had public static Dictionary<TValue, TKey> MirrorDictionary(Dictionary<TKey, TValue> InDictionary) when i needed public static Dictionary<TValue, TKey> MirrorDictionary<TKey,TValue>(Dictionary<TKey, TValue> InDictionary)
TofuBug