How to use reflection to call generic Method?

Answer

You need to use reflection to get the method to start with, then "construct" it by supplying type arguments with MakeGenericMethod:

MethodInfo method =typeof(Sample).GetMethod("GenericMethod");MethodInfo generic = method.MakeGenericMethod(myType);
generic.Invoke(this,null);

EDIT: For a static method, pass null as the first argument to Invoke. That's nothing to do with generic methods - it's just normal reflection.

All c sharp Questions

Ask your interview questions on c-sharp

Write Your comment or Questions if you want the answers on c-sharp from c-sharp Experts
Name* :
Email Id* :
Mob no* :
Question
Or
Comment* :
 





Disclimer: PCDS.CO.IN not responsible for any content, information, data or any feature of website. If you are using this website then its your own responsibility to understand the content of the website

--------- Tutorials ---