How to use reflection to call generic method?



How to use reflection to call generic method?..

Answer / Deependra Verma

"To use Reflection to call a generic method, first obtain the Type object of the generic type, then create an instance of MethodInfo for the desired method using MakeGenericMethod(). Next, create an instance of the target type and call the method using Invoke(). Here is an example:nn```csharpnType genericType = typeof(List<>);nMethodInfo methodInfo = genericType.MakeGenericMethod(typeof(int));nobject obj = Activator.CreateInstance(genericType);nobject result = methodInfo.Invoke(obj, new object[] {});n``

Is This Answer Correct ?    0 Yes 0 No

Post New Answer

More C Sharp Interview Questions

What is difference between private and static constructor?

1 Answers  


What are STA And MTA in threading?

4 Answers   Kanbay,


What is float in unity?

1 Answers  


What is the difference between writeline and write in c#?

1 Answers  


What are console applications used for?

1 Answers  


Is it possible to implement an interface to a structure? Is it possible to extend a struct? Is it possible to inherit a class to struct?

9 Answers   IBM, Logica CMG, TCS,


How do I register my code for use by classic com clients?

1 Answers  


what is uniary operators and binary operators and what is the difference

1 Answers   Protech,


What is routing in c#?

1 Answers  


In which way a two-dimensional array declared in C#?

1 Answers   Siebel,


What?s the advantage of using System.Text.StringBuilder over System.String?

1 Answers  


What is boxing? Explain its functionality?

1 Answers   Siebel,


Categories