34

If it's harder to explain using words, let's look at an example I have a generic function like this

void FunctionA<T>() where T : Form, new()
{
}

If I have a reflected type, how do I use it with the above function? I'm looking forward to do this

Type a = Type.GetType("System.Windows.Forms.Form");
FunctionA<a>();

Of cause the above method doesn't work.

4 Answers 4

17
class Program
{
    static void Main(string[] args)
    {
        int s = 38;


        var t = typeof(Foo);
        var m = t.GetMethod("Bar");
        var g = m.MakeGenericMethod(s.GetType());
        var foo = new Foo();
        g.Invoke(foo, null);
        Console.ReadLine();
    }
}

public class Foo
{
    public void Bar<T>()
    {
        Console.WriteLine(typeof(T).ToString());
    }
}

it works dynamicaly and s can be of any type

0
14

You can't. Generics in .NET must be resolved at compile time. You're trying to do something that would resolve them at runtime.

The only thing you can do is to provide an overload for FunctionA that takes a type object.


Hmmm... the commenter is right.

class Program
{
    static void Main(string[] args)
    {
        var t = typeof(Foo);
        var m = t.GetMethod("Bar");
        var hurr = m.MakeGenericMethod(typeof(string));
        var foo = new Foo();
        hurr.Invoke(foo, new string[]{"lol"});
        Console.ReadLine();
    }
}

public class Foo
{
    public void Bar<T>(T instance)
    {
        Console.WriteLine("called " + instance);
    }
}

MakeGenericMethod.

3
  • 1
    How can .NET get so close to the power of a 1958 technology and yet still be so far away? Nov 25, 2008 at 21:36
  • .NET is mainstream, Lisp (I assume that's what your reference is to) is not. That may answer the "why" question. Dec 1, 2009 at 21:36
  • 11
    That's one very definitive answer. Too bad it's incorrect. You can, through reflection, invoke generic methods using generic arguments resolved at runtime. It's not always desired, and one may wonder why he has to do it before actually using it, but it's possible.
    – Jb Evain
    Dec 1, 2009 at 21:41
7

A few years late and from a msdn blog, but this might help:

Type t = typeof(Customer);  
IList list = (IList)Activator.CreateInstance((typeof(List<>).MakeGenericType(t)));  
Console.WriteLine(list.GetType().FullName); 
0

I solved this problem in a different way. I have a list class that encapsulates the (real) Dapper functionality. It inherits from a base class that is the dummy class for mocking. Every method in the base class is overridden by the real class. Then I don't need to do anything special. If in the future, I want to do something special with SQLite or a home-grown in-memory database, I can always add that to the base class later if I wish.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.