adspace


How to use reflection to call generic method?

Answer Posted / 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       View All Answers


Please Help Members By Posting Answers For Below Questions

What is an abstract class c#?

977


How to assign Null value to Var?

1073


What is expression tree in c#?

1006


How do you inherit a class into other class in c#?

1001


Which namespaces are necessary to create a localized application?

1151


Why can't we use a static class instead of singleton?

963