3

I'm writing some unit tests and I have a lot of functions of the form

public void SomeTestHelperMethod<TKey, TValue>(TKey key, TValue value)

which I'm calling repeatedly with various arguments like this

SomeTestHelperMethod<int, int>(0, 1);
SomeTestHelperMethod<int, object>(1, new Nullable<double>(16.5));
SomeTestHelperMethod<int, string>(2, "The quick brown fox jumped over the lazy dog.");
SomeTestHelperMethod<object, int>(new NullReferenceException(), 15);
SomeTestHelperMethod<object, object>(StringComparison.Ordinal, new Version());
SomeTestHelperMethod<object, string>((ushort)3, string.Empty);
SomeTestHelperMethod<string, int>(string.Empty, 195);
SomeTestHelperMethod<string, object>("A string", this);
SomeTestHelperMethod<string, string>("Another string", "Another string");

What I'd like to do is write a function that takes an Action delegate and can Invoke the delegate with all of the different arguments. Is there any way to do it?

Answer:

Thanks to MichaelCG here's what I ended up doing:

private void CallWithKeyAndValue(string methodName)
{
    MethodInfo method = typeof(ObservableDictionaryTest).GetMethod(methodName);
    foreach (KeyValuePair<object, object> kvp in ourKeyValueSet)
    {
        MethodInfo genericMethod = method.MakeGenericMethod(kvp.Key.GetType(), kvp.Value.GetType());
        genericMethod.Invoke(this, new[] { kvp.Key, kvp.Value });
    }
}

I'd still be interested in a more general method but this one is functional for my purposes.

Bryan Anderson
  • 15,969
  • 8
  • 68
  • 83
  • You mean so you can have a list of objects and just loop on it, but actually call the specific generic version of the method? – MichaelGG Oct 29 '09 at 18:35
  • Something like that. Eventually I'd like to make some better test arguments than the random ones I made up and I'd like to be able to add it to all of the functions of this form in a single place. Right now I have a lot of duplicated code in my unit tests that I'd like to get rid of but the functions I'd like to call (SomeTestHelperMethod) are all generic functions. – Bryan Anderson Oct 29 '09 at 18:37

1 Answers1

6

If I understand you correctly, this should demonstrate what you are trying to do. The magic is in MakeGenericMethod.

using System;

class Program {
    static void Main(string[] args) {
        var meth = typeof(Program).GetMethod("Meth");
        var items = new[] { 
            new { a = (object)"hi", b = (object)1 },
            new { a = (object)TimeSpan.MaxValue, b = (object)DateTime.UtcNow },
        };
        foreach (var item in items) {
            var gmeth = meth.MakeGenericMethod(item.a.GetType(), item.b.GetType());
            gmeth.Invoke(null, new[] { item.a, item.b });
        }
    }

    public static void Meth<A, B>(A a, B b) {
        Console.WriteLine("<{0}, {1}>", typeof(A).Name, typeof(B).Name);
    }
}

Outputs:

<String, Int32> 
<TimeSpan, DateTime>
MichaelGG
  • 9,976
  • 1
  • 39
  • 82