3

I have a method like this,

public List<T> Test<T>()
{
    // do something.
}

I don't know what is T and dont have. But I have type of T as TYPE.

for example:

class Person
{

}

var type = typeof(Person);

I don't have Person. Person is keeping at the type object.

How can I use the test method ?

var list = Test<type>(); // It gives an error like this. I must use the type object.
Sinan AKYAZICI
  • 3,942
  • 5
  • 35
  • 60

5 Answers5

7

You can use the MakeGenericMethod method from MethodInfo:

MethodInfo info = this.GetType().GetMethod("Test").MakeGenericMethod(type);
object result = info.Invoke(this, null);

This is assuming you call the method inside the same type that defines Test. If you call it from somewhere else, use typeof(ClassThatDefinesTest) instead of this.GetType(), and the instance of this class instead of this.

Botz3000
  • 39,020
  • 8
  • 103
  • 127
  • Thank you for answer. I wrote simply to give a example. I'm sorry. Edited my question. I need to a return type. How can I cast after call the invoke method? – Sinan AKYAZICI Jun 14 '12 at 13:08
  • @sinanakyazici You get an object back from invoke. You can just cast that like any other object. **EDIT:** Wait. You don't have the actual type at compile time. why should you need to cast it then? – Botz3000 Jun 14 '12 at 13:09
  • It returns object. I can't use object. I need to List. I have a other problem. The method which I want to use is extension method. I can't access with GetMethods(). Because It doesnt belong object. I dont know where is it. I cant edit the Test method. Because It is comming from Framework. I have the test method this way. I must use with the type object. – Sinan AKYAZICI Jun 14 '12 at 13:27
  • @sinanakyazici Then you have to specify the appropriate `BindingFlags` to `GetMethod` to also retrieve static methods. If you need a list, you can just make a `List`. You don't know the type anyway. If you have more restrictions, you should add them in your question or better, post another question about the new problems you have to solve. – Botz3000 Jun 14 '12 at 13:39
  • Thank you very much Mr. Botz. I solved this problem by casting IEnumerable. – Sinan AKYAZICI Jun 14 '12 at 13:54
1

If typeof(T) is all you really needed, then you can refactor it to:

public void Test(Type t)
{
    // do something.
}

And call it like:

Test(type);

If this won't work for you, I recommend Botz3000's solution using MakeGenericMethod.

You could expose both Test<T>() and Test(Type) and then have one call the other, (either with MakeGenericMethod or typeof(T)) depending on whether you need the static type or simply the runtime type. That way your callers don't need to know which of the two you need.

Tim S.
  • 55,448
  • 7
  • 96
  • 122
0

You have pass the Class name to it. Please refer to the following MSDN,

http://msdn.microsoft.com/en-us/library/twcad0zb(v=vs.100).aspx

Rajesh Subramanian
  • 6,400
  • 5
  • 29
  • 42
0

As said @Botz3000 you can use MakeGenericMethod() method. But another solution could be use the dynamic keyword and the CreateInstance() method from Activator class:

public void Test(Type type)
{
    dynamic myVariable = Activator.CreateInstance(type);          
    // do something, like use myVariable and call a method: myVariable.MyMethod(); ...
}
Omar
  • 16,329
  • 10
  • 48
  • 66
-1

You call it like this:

Test<Person>();
Klaus Byskov Pedersen
  • 117,245
  • 29
  • 183
  • 222
  • This isn't the problem, he want to know how to use type to call the generic method. – Omar Jun 14 '12 at 12:46
  • @Klaus-byskov-hoffmann - I'm also thought that he didn't understand the Generic and all those fancy T, U, etc... – Alex F Jun 15 '12 at 09:55