1

I got method:

void EmberIU<T>() where T : Form
{
    for (int i = 0; i < ins.Length; i++)
        ins[i].Click += delegate { ShowForm<T>("arg")); };
}

Can I use that Method like:

var thing = Type.GetType("namespace.class");

EmberIU<thing>()
Patrick Hofman
  • 153,850
  • 22
  • 249
  • 325

2 Answers2

4

You need to use the Type.MakeGenericType(params Type[]) method (see here: http://msdn.microsoft.com/en-us/library/system.type.makegenerictype%28v=vs.110%29.aspx)

For example:

Type yourType = Type.GetType("namespace.class");
Type emberType = typeof(EmberIU<>).MakeGenericType(yourType);
Martin Costello
  • 9,672
  • 5
  • 60
  • 72
0

No you can't. The type parameter must be defined at compile time.

When you want to implement this you have to use Reflection. Take a look at Jon Skeets answer.

Community
  • 1
  • 1
Patrick Hofman
  • 153,850
  • 22
  • 249
  • 325